all groups > dotnet xml > july 2003 >
You're in the

dotnet xml

group:

Is it possible to serialize a read only property


Re: Is it possible to serialize a read only property Dino Chiesa [MSFT]
7/30/2003 12:06:20 PM
dotnet xml:
By default, no, you cannot serialize readonly properties to XML .

But what do you really want to do? What's the end goal? There may be a way
to accomplish what you want.

If you serialize with the BinaryFormatter, you can get all obj properties.
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemRuntimeSerializationFormattersBinaryBinaryFormatterClassTopic.asp

Alternatively, staying with the XmlSerializer, you could use a hack where
you provide a public setter method, which does nothing. In this case the
XML Serialization will work, but de-serialization may not behave the way you
want.

public class MyClass {
private int _myInt;
public int MyInt {
get { return _myInt; }
set { } // do nothing!!
}
}

But understand that in this case, at De-serialize time, your data will
disappear. The XmlSerializer.Deserialize() method will call your setter,
which will do nothing (by your design) and then the data from the XML file
will NOT be set in the (deserialized) object instance.

Also you will have confusing code that may be hard to use or hard to
maintain later: Users of your class may invoke the setter expecting it to
actually work.


-Dino



[quoted text, click to view]

Is it possible to serialize a read only property Mudit
7/30/2003 6:45:28 PM
Hi,

I am new to XML Serialization. I am interested in serializing my object to
Xml. I only have to serialize and I needn't worry about deserialization.
As per my understanding, only the properties that have both "get" and "set"
can be serailized. Is that so. Is it also possible to serialize properties
that are read only.

Thanks,
Mudit

Re: Is it possible to serialize a read only property Mudit
7/31/2003 9:30:38 AM
Thanks Dino,

My goal is just to get an XML representation of some of the properties of my
object. The XML generated is input to another system. This is just a one-way
process and I do not need to deserialize the object.

Currently I have provided set methods that do nothing, but I am looking for
a better solution. Would writing a new serializer be the only solution in
that case.

Mudit


[quoted text, click to view]

Re: Is it possible to serialize a read only property Oleg Tkachenko
7/31/2003 10:30:01 AM
[quoted text, click to view]

Another interesting solution could be using ObjectXPathNavigator [1] to
navigate your object and serialize it using XSLT (you can also add any
arbitrary serialization logic there).

[1] http://msdn.microsoft.com/library/en-us/dnexxml/html/xml03172003.asp
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Re: Is it possible to serialize a read only property Mudit
7/31/2003 6:38:45 PM
Thanks,

Thats a really cool feature. I didn't know something like this existed.

Mudit

[quoted text, click to view]

AddThis Social Bookmark Button