469,631 Members | 1,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

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 1903
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 IXmlSerializable, 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**************@TK2MSFTNGP10.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*******@discussions.microsoft.com> wrote in message
news:2F**********************************@microsof t.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 IXmlSerializable, 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Anthony Bouch | last post: by
3 posts views Thread by Bob Rundle | last post: by
4 posts views Thread by Steve Long | last post: by
1 post views Thread by Eugene Vtial | last post: by
3 posts views Thread by Tantr Mantr | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.