System.TimeSpan XML Serialization - by FrankT-Qc

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 386602 Comments
Status Closed Workarounds
Type Bug Repros 8
Opened 12/4/2008 2:36:59 PM
Access Restriction Public


The type System.TimeSpan does not serialize properly when used with XmlSerializer.

Actually it does not serialize at all...

By the way, XmlSerializer comes from 
Sign in to post a comment.
Posted by StuartBrown on 5/21/2012 at 2:02 AM
Regardless of there being a workaround this is an open bug which deplayed a project for several weeks - we purchased access to a 3rd party API and this bug blanked the fields. We raised a ticket with the supplier but they were slow to respond.

Either support TimeSpan or raise an error if you encounter it; silently losing data is unacceptable.
Posted by Harry-vB on 12/20/2011 at 2:31 AM
Why can't this be fixed? TimeSpan is one of the most important types; not supporting it leads people to abuse DateTime for timespans.

This thread is marked as "Closed as fixed", but the issue isn't fixed. The (helpful!) answer given by Steve Maine below is only a workaround and should be marked accordingly.

Please take a look at to see, how this makes people angry.

Posted by aschmidt on 12/16/2011 at 2:11 PM
.Net 4.0 does not work as described below, see example code for repro:

[DataContract(Name = "SetTest")]
    public class SerTest
        public int B;
        public TimeSpan A;

    class Program
        static void Main(string[] args)
            var sb = new StringBuilder();

            var o = new SerTest();
            o.A = TimeSpan.FromHours(1);
            var ser = new XmlSerializer(typeof(SerTest));
            ser.Serialize(new StringWriter(sb), o);

Posted by Eric Cosky on 3/20/2011 at 11:40 AM
This isn't fixed; it silently fails to serialize with .NET 4. I would prefer it threw an exception when encountering types it doesn't support. While other APIs are available that can be used to serialize TimeSpan objects, this particular API does not work for it and it does not throw an exception like it does when encountering other unsupported types.
Posted by Microsoft on 2/18/2009 at 2:24 PM
It's true that the XmlSerializer does not directly support System.TimeSpan as a primitive. You need to do a small amount of work via System.Xml.XmlConvert, like so:

public class clTestClass
        public string SomeString;

        public TimeSpan SomeTimeAsTimeSpan;

        [XmlElement( DataType = "duration" )]
        public string SomeTime
                return XmlConvert.ToString(this.SomeTimeAsTimeSpan);

                this.SomeTimeAsTimeSpan = XmlConvert.ToTimeSpan(value);

        private clTestClass() { }

        public clTestClass(string aSomeString, System.TimeSpan aSomeTime)
            SomeString = aSomeString;
            this.SomeTimeAsTimeSpan = aSomeTime;

Alternatively, you can look at using the .NET 3.0 DataContractSerializer which fully supports TimeSpan.

Steve Maine
Program Manager, Microsoft
Posted by Microsoft on 12/4/2008 at 9:16 PM
Thanks for your feedback.

We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Thank you,
Visual Studio Product Team