Serializing to XML is easy in .NET.
All you have to do is make sure you have a default constructor and all the
fields you want to serialise accessible either directly or through properties.
Well, not quite. There are also restrictions regarding the values of the fields
that are serialised. To be more precise, some characters will cause
de-serialisation to fail with an InvalidOperationException even though
serialisation was successful.
It is important to understand that
these restrictions are not posed by the .NET implementation of XML
serialisation, but rather by the standard itself (you can have a look at the XML specification for the exact details on valid
character ranges). In other words this should not be a problem, as you should
not have XML containing such characters. It is just annoying that you are not
aware of the issue until the de-serialisation process. Having said that, it is
possible to successfully de-serialize XML that contains illegal characters
using XmlTextReader and passing an instance of it to the XmlSerializer. Here is
an example:
XmlSerializer
deSerializer = new XmlSerializer(typeof(SomeClass));
XmlTextReader
xmlTextReader = new XmlTextReader(someStream);
SomeClass comeClass =
(SomeClass) deSerializer.Deserialize(xmlTextReader);