XMLSerializer not serializing DateTime

c#, xml-serialization

Solution

Pavel's comment was the correct answer.

Also, you say "omitted other fields for clarity". Is there, by chance, a field or property named commentDateSpecified among those fields?

Problem

Input...note that comment is generated code from the xsd tool. It's in a 31,834 line file and proprietary, but I put a rough approximation in here. ``` [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")] [System.SerializableAttribute()] [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] public class comment { private System.DateTime commentDateField; private bool commentDateFieldSpecified; [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public System.DateTime commentDate { get { return this.commentDateField; } set { this.commentDateField = value; } } [System.Xml.Serialization.XmlIgnoreAttribute()] public bool commentDateSpecified { get { return this.commentDateFieldSpecified; } set { this.commentDateFieldSpecified = value; } } //other fields omitted for clarity } comment c = new comment(); c.text = txtComment.Text; c.commentDate = DateTime.Now; StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); XmlSerializer xs = new XmlSerializer(typeof(comment)); xs.serialize(sw as TextWriter, c); string output = sb.ToString(); ``` output-> ``` <comment> <text>My Comment Text</text> </comment> ``` Where's the date?

Original source