December 15, 2004
@ 12:37 PM

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;

[XmlElement("Interval", DataType="duration")]
public string IntervalXml
{
    get
    {
        if (Interval < TimeSpan.FromSeconds(1) )
        {
            return null;
        }
        else
        {
            return XmlConvert.ToString(interval);
        }
    }
    set
    {
        if (value == null )
        {
            interval = TimeSpan.Zero;
            return;
        }
        TimeSpan newInterval = XmlConvert.ToTimeSpan(value);
        if (interval == newInterval)
            return;
        interval = newInterval;
    }
}

[XmlIgnore]
public TimeSpan Interval
{
    get
    {
        return interval;
    }
    set
    {
        if (interval == value)
            return;
        interval = value;
    }
}

 

 

Categories: XML

Wednesday, December 15, 2004 9:48:06 PM UTC
I had an irritating bug in RSS Bandit due to the fact that XmlSerializer doesn't serialize TimeSpans. What's really annoying is that instead of erroring it writes an empty element.

Grrrr.
Wednesday, December 15, 2004 10:02:09 PM UTC
http://dotnetjunkies.com/WebLog/bsblog/archive/2004/08/10/21612.aspx

I posted this a while ago. Admittedly a hack compared with using duration. Nice work!
Thursday, December 16, 2004 12:01:17 AM UTC
Here is a similar hack that i have been usind for datetime and timespan.

private DateTime /*TimeSpan*/ _when; //date and time
[XmlIgnore]
public DateTime When {
get { return _when; }
set { _when = value; }
}

[XmlAttribute("When")] //[XmlElement] if you wish
public string WhenText {
// You can use any format that you want here.
get { return string.Format("{0:mm/dd/yyyy hh:mm:ss", _when); }
// The trick is in using the 'Parse' function of DateTime and Timespan.
set { _when = DateTime.Parse(value); }
}

Now even partial seconds will work.
Eddie Garmon
Thursday, December 16, 2004 12:01:20 AM UTC
Here is a similar hack that i have been usind for datetime and timespan.

private DateTime /*TimeSpan*/ _when; //date and time
[XmlIgnore]
public DateTime When {
get { return _when; }
set { _when = value; }
}

[XmlAttribute("When")] //[XmlElement] if you wish
public string WhenText {
// You can use any format that you want here.
get { return string.Format("{0:mm/dd/yyyy hh:mm:ss", _when); }
// The trick is in using the 'Parse' function of DateTime and Timespan.
set { _when = DateTime.Parse(value); }
}

Now even partial seconds will work.
Eddie Garmon
Wednesday, December 22, 2004 12:44:14 PM UTC
I'll talk to Eugene & Elena about it.
Friday, December 16, 2005 1:32:55 AM UTC
Been trying to work out how to do this for a few hours, thanks.
Comments are closed.