Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

XmlSerializer and the "Specified" suffix

It took me 3 hours to figure out why XmlSerializer wasn’t writing out a particular class property. This is why:

…if a serializable Field/Property has a corresponding field of type Boolean having as a name the Field/Property name with "Specified" suffix, the XmlSerializer conditionally exclude that Field/Property from the serialization process.

DateTime Serialization to XML

I have an C# serializable object with a DateTime property like this:

        public class MyClass {
public DateTime Date1 {get;set;}
}

When serialized to XML, it gives me something like this


        <MyClass>
<Date1>2010-11-30T00:00:00</Date1>
</MyClass>

I could not get it to give me this, which is in the proper  xml date format:


        <MyClass>
<Date1>2010-11-30</Date1>
</MyClass>

After a couple of wasted days, it turned out that the solution is really simple:


        public class MyClass {
[System.Xml.Serialization.XmlElement("Date1", DataType="date")]
public DateTime Date1 {get;set;}
}