Can't serialize a TimeSpan to XML

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I can't get a TimeSpan to serialize properly to XML, despite the fact
that the documentation says that it is serializable. This class:

public class MyClass
{
TimeSpan _TimeSpan = new TimeSpan(1,2,3,4);
public TimeSpan ATimeSpan
{
get { return _TimeSpan; }
set { _TimeSpan = value; }
}
public int AnInt
{
get { return 99; }
set { }
}
}

should be serialized with this code:

MyClass myClass = new MyClass();
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
StringWriter writer = new StringWriter();
serializer.Serialize(writer,myClass);
writer.Close();
textBox1.Text = writer.GetStringBuilder().ToString();

but the timespan object is empty:

<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ATimeSpan />
<AnInt>99</AnInt>
</MyClass>

Am I doing something obviously stupid?
 
Pete S said:
I can't get a TimeSpan to serialize properly to XML, despite the fact
that the documentation says that it is serializable. This class:

public class MyClass
{
TimeSpan _TimeSpan = new TimeSpan(1,2,3,4);
public TimeSpan ATimeSpan
{
get { return _TimeSpan; }
set { _TimeSpan = value; }
}
public int AnInt
{
get { return 99; }
set { }
}
}

but the timespan object is empty:

<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ATimeSpan />
<AnInt>99</AnInt>
</MyClass>

Am I doing something obviously stupid?

Hello,
the trick with xml serialization is to provide "fake" properties. See the
following code.

[Serializable]
public class SampleStructure
{
// Your "real" property
[XmlIgnore]
public TimeSpan Duration;

// The additional "fake" property
[XmlElement("Duration")]
public string XmlDuration
{
get { return Duration.ToString(); }
set { Duration = TimeSpan.Parse(value); }
}
}

HTH,
Mark
 
Thanks, Mark, I can see that this would work around the problem. What I don't
understand is why I need to work around it in the first place. Isn't it a
bug? Or is there a design rationale behind it? I see it isn't any different
in the upcoming 2.0 either.

Pete

the trick with xml serialization is to provide "fake" properties. See the
following code.

[Serializable]
public class SampleStructure
{
// Your "real" property
[XmlIgnore]
public TimeSpan Duration;

// The additional "fake" property
[XmlElement("Duration")]
public string XmlDuration
{
get { return Duration.ToString(); }
set { Duration = TimeSpan.Parse(value); }
}
}

HTH,
Mark
 
Pete S said:
Thanks, Mark, I can see that this would work around the problem. What I
don't
understand is why I need to work around it in the first place. Isn't it a
bug? Or is there a design rationale behind it? I see it isn't any
different
in the upcoming 2.0 either.

Pete,
The XML schema definition does include three different formats for
date/time: date, dateTime and time; it also includes one for a timespan:
duration.

According to the docs, duration maps to a string. Sadly, the TimeSpan
structure doesn't support automatic conversion like DateTime does. That's
not a bug, it's "by design" ;) You could post a suggestion at
http://lab.msdn.microsoft.com/productfeedback/ Then, maybe the next version
of the framework will include this feature.

BTW, I've forgotten the DataType. Change to code to
[XmlElement("Duration", DataType = "duration")]
public string XmlDuration
{
get { return Duration.ToString(); }
set { Duration = TimeSpan.Parse(value); }
}

Mark
 
Back
Top