If you've tried to XML serialize a System.TimeSpan value, you may have noticed that your node is null, no matter what you set the TimeSpan value to be. There's a nice workaround to this posted here by Ben Lucas. Basically, you [XmlIgnore] your object's TimeSpan property, and add another that serializes the time span's ticks. Pretty simple, but works great!
[XmlIgnore]
public System.TimeSpan TimeToBeReceived
{
get { return timeToBeReceived;}
set
{
if (timeToBeReceived == value) return;
timeToBeReceived = value;
}
}
public long TimeToBeReceivedTicks
{
get { return timeToBeReceived.Ticks; }
set { this.timeToBeReceived = new TimeSpan(value); }
}
So instead of this:
<TimeToBeReceived />
You get this:
<TimeToBeReceivedTicks>864000000000</TimeToBeReceivedTicks>
Thanks Ben!
-Brendan
Posted
Tue, Aug 10 2004 9:07 AM
by
Brendan Tompkins