PC Review


Reply
Thread Tools Rate Thread

Can't serialize a TimeSpan to XML

 
 
=?Utf-8?B?UGV0ZSBT?=
Guest
Posts: n/a
 
      20th May 2005
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?
 
Reply With Quote
 
 
 
 
Mark Rockmann
Guest
Posts: n/a
 
      20th May 2005
"Pete S" <(E-Mail Removed)> schrieb im Newsbeitrag
news:449BC487-7189-48B4-8D92-(E-Mail Removed)...
>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


 
Reply With Quote
 
=?Utf-8?B?UGV0ZSBT?=
Guest
Posts: n/a
 
      20th May 2005
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

"Mark Rockmann" wrote:

> "Pete S" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:449BC487-7189-48B4-8D92-(E-Mail Removed)...
> >I can't get a TimeSpan to serialize properly to XML, despite the fact
> > that the documentation says that it is serializable. This class:
> > snip snip


> 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
>
>
>

 
Reply With Quote
 
Mark Rockmann
Guest
Posts: n/a
 
      20th May 2005
"Pete S" <(E-Mail Removed)> schrieb im Newsbeitrag
news97854BD-AB32-43EE-8F50-(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TimeSpan in C# prasanta.bhowmik@gmail.com Microsoft C# .NET 5 3rd Apr 2007 03:52 PM
serialize to SQL Server Blob instead of XML serialize Gordz Microsoft ASP .NET 3 7th Jun 2004 09:46 PM
Using Timespan... Beringer Microsoft Dot NET Framework 3 4th Jun 2004 07:11 PM
TimeSpan Lou Microsoft C# .NET 3 23rd Dec 2003 10:08 AM
TimeSpan Nigel Findlater Microsoft Dot NET Framework 3 8th Dec 2003 05:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:50 PM.