2

I have an XML string with the following date: 2001-01-01T03:40:00Z

Note how it ends with Z, to indicate UTC.

I deserialize like so:

using (StringReader stringReader = new StringReader(xmlString))
    using (XmlReader xmlReader = XmlReader.Create(stringReader))
        obj = (MyObject)sr.ReadObject(xmlReader);

But when I go to the corresponding DateTime fields in the resulting object, the Kind field is set to "Unspecified" rather than "Utc" which is what it should be. How do I work around this bug?

ATDeveloper
  • 289
  • 4
  • 15

1 Answers1

2

The Kind field doesn't have a setter so the XmlSerializer won't be able to set it.

Many recommend always serializing the time as UTC and then calling ToLocalTime if you need it.

See this.

Community
  • 1
  • 1
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • 1
    This is how we do it, and it works pretty well. The data itself always remains generic, and any views can reflect whatever time zone the system is set to. – drharris Mar 12 '11 at 07:16