There’s a lot of information (including sample routines) on how to serialize/deserialize objects in C#. Hunt down a couple of routines that accept generic types and add them to your personal utility class and reuse them forever. Be aware, however, that there’s an issue with serializing nullable types. Basically it can’t be done if an object’s property is marked with an XmlAttribute. You will need to mark the property as an XmlElement.
[XmlElement(IsNullable=true)] public DateTime? MachineDateChange { get ; set ; }
And use some caution as the property datatype must match the IsNullable setting for the XmlElement markup. Believe it or not, the following “type mismatch” will throw an exception when serializing since MachineDateChange, in the following case, isn’t actually nullable like it is above:
[XmlElement(IsNullable = true)] public DateTime MachineDateChange { get; set; }
Here are a couple of good resources:
April 23rd, 2009 at 10:03 pm
Thats great, however the XML serializer still emits the element with xsi:nil=”true”. Any idea how to supress it, if it is null.
Thanks