Dear All,
Hope somebody can help me with this.
I have created a class and have got it to serialize and deserialize using
XmlSerialization without any problems. I've also read the documentation on
the XML attribute classes such as XmlElement and XmlRoot etc as well as
XmlAttributeOverrides.
However, I run into a problem if I need to perform a conversion on a
property before serializing it. For example, if I have a class with a
property called SomeProperty with a type of Integer. If SomeProperty's value
is 0, I don't want to serialize it but if it's not zero, I want to serialize
it.
Or, another example would be, if I have a class with a property of integer
and I need to serialize the output to XML to convert the value to a string.
ie SomeProperty = 5 and I need the XML output to give me a string "Five"
instead of the default conversion value of "5".
My thinking is that there should be an easy way of applying an attribute to
the property that translates the output of the property to XML and
vice-versa. It would be great if there were an attribute called
XMLConvertAttribute that will specify the conversion to be performed to XML.
But I can't find any information on this. Is there any other easy way?
I know that I could always customize my XML serialization by using
Surrogates or by using IXMLSerializable interface or by just using the quick
and dirty way, by using a function to read all the values in the class and
build the XML by hand. However, all these solutions forces me to customize
serialization for the entire class while all I need is to perform custom
conversion on a couple of properties. Also, I wanted a good object-oriented
method and I just thought somewhere somehow, somebody must have a neat
solution to this problem.
Thanks a lot for any help.