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;}
}

0 comments: