Groups | Blog | Home
all groups > dotnet xml > may 2004 >

dotnet xml : XmlSerializer - public readonly properties not in XML


gert NO[at]SPAM up-front.co.za
5/6/2004 3:37:04 AM
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?
Dino Chiesa [Microsoft]
5/6/2004 2:09:31 PM
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



[quoted text, click to view]

Oleg Tkachenko [MVP]
5/6/2004 2:26:32 PM
[quoted text, click to view]

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

--
Oleg Tkachenko [XML MVP]
Phillip Evans
5/17/2004 10:43:35 AM
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.


[quoted text, click to view]

AddThis Social Bookmark Button