I was looking at my referrer logs today, and noticed that my blog came up in a search on "XML Serializer read only property" -- which is funny in some ways, because I've never written anything about that topic. I have had to solve that problem, though.
The XML Serializer will only operate on publicly visible read/write properties or fields. Anything that's not public or not read/write will be invisible to the XML serialization mechanism -- and there's not much you can do about it. It would be great if this type of thing could be controlled via an attribute or something, but to my knowledge it's not...there's not much you can do except make sure that any property you want exposed is read/write.
Actually, that's not quite the requirement -- each property exposed to the XML serializer has to have both a getter and a setter defined. There's no requirement for that setter to actually do anything, however. One way to serialize a read-only property in XML would be to implement that property as follows:
public string MyReadOnlyString
{
get{ return "I dare you to try and change this!"; }
set{ }
}
MyReadOnlyString will be present in the XML representation, but the user cannot change it's value. Properties that need to be write-once can write directly to the property backer during construction to get the same effect.
The downside to this is that the property will look read/write to the client, who will probably expect it to behave differently than it really does. This can be addressed out-of-band (ugh) through documentation.
