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/frlrfSystemRuntimeSerializati
onFormattersBinaryBinaryFormatterClassTopic.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
> Hi,
>
[quoted text clipped - 6 lines]
> Thanks,
> Mudit
Mudit - 31 Jul 2003 01:30 GMT
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
> 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/frlrfSystemRuntimeSerializati
onFormattersBinaryBinaryFormatterClassTopic.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
[quoted text clipped - 31 lines]
> > Thanks,
> > Mudit
Oleg Tkachenko - 31 Jul 2003 09:30 GMT
> 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
[quoted text clipped - 3 lines]
> a better solution. Would writing a new serializer be the only solution in
> that case.
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

Signature
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Mudit - 31 Jul 2003 10:38 GMT
Thanks,
Thats a really cool feature. I didn't know something like this existed.
Mudit
> > 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
[quoted text clipped - 9 lines]
>
> [1] http://msdn.microsoft.com/library/en-us/dnexxml/html/xml03172003.asp