T
The Last Danish Pastry
Why is it that I can serialize a DateTime using XmlSerializer, but not
a TimeSpan?
Also, and perhaps more importantly - how could I have known ahead of
time, using the published documentation, that DateTime is going to
work and TimeSpan isn't?
Trying to serialize a TimeSpan does not throw any exceptions or
trigger any events.
Here is some code that produces two files of xml...
=========================================
private void button1_Click(object sender, EventArgs e)
{
{
DateTime testObject = new DateTime(2000, 12, 25, 1, 2, 3);
XmlSerializer xs = new XmlSerializer(testObject.GetType());
StreamWriter sw = new StreamWriter(@"c:\qaz\DateTime.xml");
xs.Serialize(sw, testObject);
sw.Close();
}
{
TimeSpan testObject = new TimeSpan(1, 2, 3, 4, 5);
XmlSerializer xs = new XmlSerializer(testObject.GetType());
StreamWriter sw = new StreamWriter(@"c:\qaz\TimeSpan.xml");
xs.Serialize(sw, testObject);
sw.Close();
}
} // button1_Click
=========================================
c:\qaz\DateTime.xml can be used to correctly deserialize the original
DateTime object.
The file c:\qaz\DateTime.xml...
=========================================
<?xml version="1.0" encoding="utf-8"?>
<dateTime>2000-12-25T01:02:03</dateTime>
=========================================
c:\qaz\TimeSpan.xml contains no information about the TimeSpan object,
which thus cannot be deserialized.
The file c:\qaz\TimeSpan.xml...
=========================================
<?xml version="1.0" encoding="utf-8"?>
<TimeSpan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
=========================================
a TimeSpan?
Also, and perhaps more importantly - how could I have known ahead of
time, using the published documentation, that DateTime is going to
work and TimeSpan isn't?
Trying to serialize a TimeSpan does not throw any exceptions or
trigger any events.
Here is some code that produces two files of xml...
=========================================
private void button1_Click(object sender, EventArgs e)
{
{
DateTime testObject = new DateTime(2000, 12, 25, 1, 2, 3);
XmlSerializer xs = new XmlSerializer(testObject.GetType());
StreamWriter sw = new StreamWriter(@"c:\qaz\DateTime.xml");
xs.Serialize(sw, testObject);
sw.Close();
}
{
TimeSpan testObject = new TimeSpan(1, 2, 3, 4, 5);
XmlSerializer xs = new XmlSerializer(testObject.GetType());
StreamWriter sw = new StreamWriter(@"c:\qaz\TimeSpan.xml");
xs.Serialize(sw, testObject);
sw.Close();
}
} // button1_Click
=========================================
c:\qaz\DateTime.xml can be used to correctly deserialize the original
DateTime object.
The file c:\qaz\DateTime.xml...
=========================================
<?xml version="1.0" encoding="utf-8"?>
<dateTime>2000-12-25T01:02:03</dateTime>
=========================================
c:\qaz\TimeSpan.xml contains no information about the TimeSpan object,
which thus cannot be deserialized.
The file c:\qaz\TimeSpan.xml...
=========================================
<?xml version="1.0" encoding="utf-8"?>
<TimeSpan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
=========================================