By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,196 Members | 1,682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,196 IT Pros & Developers. It's quick & easy.

XmlSerializer - public readonly properties not in XML

P: n/a
Hi, I know the MSDN documents etc specify that readonly properties of
an object is not included in the XML that is generated - but any idea
why?

This put some rather severe limitations on how you must implement the
classes you wish to serialize with, and make the tool inpractical -
Most of the cases you will have readonly properties on a class...

Any easy work around?

IXmlSerializable?
XPath Querying Over Objects with ObjectXPathNavigator?
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Straatvark wrote:
Hi, I know the MSDN documents etc specify that readonly properties of
an object is not included in the XML that is generated - but any idea
why?


And how do you think they are gonna be set when constructing object
during deserialization?

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2

P: n/a
My easy workaround for this is to use a no-op setter.
Eg,

public class BA {
public ReportGroup[] ReportGroup;
public string ID;

private string m_Internal;

public string ReadOnlyProp {
set {}
get {return m_Internal;}
}
}
This will serialize the readonly property correctly.
However on de-serialization, the property will not be set, because the
setter does nothing.

Be aware that serialization, then de-serialization, of this type of object
will lose data.
-Dino

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:e5**************@TK2MSFTNGP09.phx.gbl...
Straatvark wrote:
Hi, I know the MSDN documents etc specify that readonly properties of
an object is not included in the XML that is generated - but any idea
why?


And how do you think they are gonna be set when constructing object
during deserialization?

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #3

P: n/a
Why do you assume that an object will always be deserialized within a .NET
environment? We have several objects that we send to our customers using a
web service that we never EVER deserialize ourselves and yet we can't use
the .NET default serialization because our read-only properties are not
serialized.

Is there some way to override the built-in behavior and tell .NET to
serialize read-only properties (because we understand that we can't
deserialize the object but we don't want to anyway so don't care)?

Phil.
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:e5**************@TK2MSFTNGP09.phx.gbl...
Straatvark wrote:
Hi, I know the MSDN documents etc specify that readonly properties of
an object is not included in the XML that is generated - but any idea
why?


And how do you think they are gonna be set when constructing object
during deserialization?

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.