2

I have an XML document I'm deserializing using XmlSerializer. The other properties on the document work just fine, but there is one element in particular that isn't working:

<StartDate>2014-03-21T00:00:00</StartDate>

This is defined in C# in a POCO as:

[XmlElement("StartDate")]
private DateTime StartDate { get; set; }

When I deserialize the document, StartDate is 01/01/0001 12:00:00 AM. I thought the standard XML date/time format was YYYY-MM-DDTHH:MM:SS? So why is this not deserializing correctly?

Note: I do not have control over the XML document I'm deserializing. It's coming from a third-party system.

qJake
  • 16,821
  • 17
  • 83
  • 135

1 Answers1

7

Change the access modifier from private to public:

[XmlElement("StartDate")]
public DateTime StartDate { get; set; }

XmlSerializer can only serialize public fields and properties.

Michael Liu
  • 52,147
  • 13
  • 117
  • 150
  • Oh wow, how did I miss that? All my other properties were public. Stupid mistake! Thanks! – qJake Mar 28 '14 at 16:02