XmlSerializer and TimeSpan
Just had to figure this out and thought I’d share. With the XmlSerializer (.NET v1.1), one would think that TimeSpan maps to the XML Schema type duration, but it doesn’t – for whatever reason. Anyways … here’s a trick to make it work. Interestingly enough, the XmlConvert class understands TimeSpan. However it does not work correctly with fractional seconds and ignores them. That’s enough for my purposes in the given app and therefore I am ignoring that issue in the snippet below and treat all times of less than one second as equivalent to zero. If it isn’t enough for you, you’d have to write an alternate implementation for the respective XmlConvert functionality or beg Microsoft to fix it. (Doug? ;-)
private TimeSpan interval; |