473,699 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializer - Serialize properties with get accessors?

Hello everyone,

I would like to serialize an object to XML. Currently my code will serialize
all of the public properties that are value types, but not my public
properties that have get accessors. e.g:

public class MyObject
{
public string name; // This is serialized OK

public int Two
{
get { return 1+1;} // This does not serialize
}

}

Is it possible to serialize these properties too? If so could you please
tell me how (or point me in the direction of a doc that explains it).
I'm fairly new to working with the Xml parts of the framework so I'm sorry
if I've missed something obvious.

Thanks very much!

Peter Cresswell
p-*********@iname .com

Nov 16 '05 #1
3 2068
From what I know, you have 2 options:

1. Implement set accessors that do nothing (not very pretty I know):

public int Two
{
get { return 1+1;}
set { }
}

2. Implement IXmlSerializabl e, which is not currently intended for developer use, but this is being changed in 2.0. This interface allows you to control exactly how your objects are serialized/deserialized through 3 methods - ReadXml, WriteXml and GetSchema.

Hope this helps
Dan
"Peter Cresswell" wrote:
Hello everyone,

I would like to serialize an object to XML. Currently my code will serialize
all of the public properties that are value types, but not my public
properties that have get accessors. e.g:

public class MyObject
{
public string name; // This is serialized OK

public int Two
{
get { return 1+1;} // This does not serialize
}

}

Is it possible to serialize these properties too? If so could you please
tell me how (or point me in the direction of a doc that explains it).
I'm fairly new to working with the Xml parts of the framework so I'm sorry
if I've missed something obvious.

Thanks very much!

Peter Cresswell
p-*********@iname .com

Nov 16 '05 #2
Has to be read/write. Says in the docs. Could not deserialize if no set
accessor.

--
William Stacey, MVP

"Peter Cresswell" <p-*********@iname .com> wrote in message
news:u0******** ******@TK2MSFTN GP10.phx.gbl...
Hello everyone,

I would like to serialize an object to XML. Currently my code will serialize all of the public properties that are value types, but not my public
properties that have get accessors. e.g:

public class MyObject
{
public string name; // This is serialized OK

public int Two
{
get { return 1+1;} // This does not serialize
}

}

Is it possible to serialize these properties too? If so could you please
tell me how (or point me in the direction of a doc that explains it).
I'm fairly new to working with the Xml parts of the framework so I'm sorry
if I've missed something obvious.

Thanks very much!

Peter Cresswell
p-*********@iname .com


Nov 16 '05 #3
The other one is create simple xml "doc" classes that only have public
fields with any xml attributes you need. That way you not messing with
these kind of issues with your app classes. Create a method in your real
class to "clone" your object to its' xml class and serialize that class
instead, etc.

--
William Stacey, MVP

"Dan Kelley" <Da*******@disc ussions.microso ft.com> wrote in message
news:2F******** *************** ***********@mic rosoft.com...
From what I know, you have 2 options:

1. Implement set accessors that do nothing (not very pretty I know):

public int Two
{
get { return 1+1;}
set { }
}

2. Implement IXmlSerializabl e, which is not currently intended for developer use, but this is being changed in 2.0. This interface allows you
to control exactly how your objects are serialized/deserialized through 3
methods - ReadXml, WriteXml and GetSchema.
Hope this helps
Dan
"Peter Cresswell" wrote:
Hello everyone,

I would like to serialize an object to XML. Currently my code will serialize all of the public properties that are value types, but not my public
properties that have get accessors. e.g:

public class MyObject
{
public string name; // This is serialized OK

public int Two
{
get { return 1+1;} // This does not serialize
}

}

Is it possible to serialize these properties too? If so could you please
tell me how (or point me in the direction of a doc that explains it).
I'm fairly new to working with the Xml parts of the framework so I'm sorry if I've missed something obvious.

Thanks very much!

Peter Cresswell
p-*********@iname .com


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7000
by: Anthony Bouch | last post by:
Hi I've been reading using the XmlSerializer with custom collections. I've discovered that when serializing a custom collection (a class that implements ICollection, IList etc.) the XmlSerializer will only serialize the collection items - with the default root as ArrayofMyItems etc. My custom collection class has some additional public properties that I would like to include in the serialization above the items element array (in
3
9032
by: Bob Rundle | last post by:
I'm trying to serialize a class with XmlSerializer. This class implements the IEnumerable interface. I implemented the IEnumerable interface for reasons other than Xml serialization. However I find that the XmlSerializer, because I have an IEnumerable interface on this class, wants to ignore the public properties of this class and simply serialize an array of objects. I wand XmlSerializer to ignore the IEnumerable interface that I...
3
6501
by: Peter Cresswell | last post by:
Hello everyone, I would like to serialize an object to XML. Currently my code will serialize all of the public properties that are value types, but not my public properties that have get accessors. e.g: public class MyObject { public string name; // This is serialized OK
4
1990
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is working fine but when I try to instantiate an XmlSerializer object with: Dim xmls As New XmlSerializer(GetType(CLayerDefinition)) I get the following error:
1
348
by: Eugene Vtial | last post by:
I have been reading the docs on this but I am just not getting it. Can anyone provide a small sample of using this class to save a simple Button to a file and then recreate it from the file?
8
5452
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the schema using xsd.exe, which took a while because xsd doesn't handle recursive elements very well (StackOverFlow exception during generation). Now that I have the classes I am trying to serialize them to an xml document to send to ArcIMS to generate...
2
5022
by: Jinsong Liu | last post by:
I have following 3 classes public class MyMainClass { MyCollection<MyObject> m_oMyObjectCollection = null; private string m_sID = string.Empty; public MyCollection<MyObject> Collection {
3
6538
by: Tantr Mantr | last post by:
Hello , I have a class which I serialize using XMLSerializer. This class has public properties which are based on other interfaces. Because of this I am unable to serialize the object. Error : There was an error reflecting the type XXXX Is there any way to serialize this object w/o moving away from Interface based classes. Many Thanks! Eg. //=========Interfaces===============
6
3815
by: Beorne | last post by:
I have to use XmlSerializer to serialize a class and I've found big problems serializing properties and indexers. I assumed that if you serialize a class with public properties (or an indexers) containing a field, even private, the serialization process would serialize that field too assuming it is needed to that public property. This is not the case ... For example I want to serialize the following class:
2
10139
by: Beorne | last post by:
I'm serializing a big class using XmlSerializer (I need xml serialization cause compact framework). I've learn that all my fields must be public. Ok. Now it is completly useless to serialize my get/set properties, but I haven't found an anttribute like working foer properties. Any help?
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.