Home Dashboard Directory Help
Search

System.TimeSpan XML Serialization by FrankT-Qc


Status: 

Closed
 as Fixed Help for as Fixed


12
0
Sign in
to vote
Type: Bug
ID: 386602
Opened: 12/4/2008 2:36:59 PM
Access Restriction: Public
0
Workaround(s)
view
8
User(s) can reproduce this bug

Description

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
[...]\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll
Details
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 http://kennethxu.blogspot.com/2008/09/xmlserializer-doesn-serialize-timespan.html to see, how this makes people angry.

Cheers,
Harry
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
    {
        [DataMember]
        public int B;
        [DataMember]
        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);

            Console.WriteLine(sb.ToString());    
        }
    }
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;

        [XmlIgnore]
        public TimeSpan SomeTimeAsTimeSpan;

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

            set
            {
                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.

Thanks
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
Sign in to post a workaround.