XmlSerializer again

  • Thread starter The Last Danish Pastry
  • Start date
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" />
=========================================
 
M

Marc Gravell

Since you've asked this, what, 3 times - I think we can assume that
nobody has a satisfactory answer.

Yes it is a known issue; it has been there a while. You could log a
bug on connect.microsoft.com to see if anybody there says anything
useful, or you could work around it - by either exposing this as a
DateTime (only using the TimeOfDay part), an integer (the minutes /
seconds / whatever resolution you need) or as a string.

Not great, but that is how it is at time moment via XmlSerializer.

Mar
 
C

Cubicle Snowman

The Last Danish Pastry said:
Why is it that I can serialize a DateTime using XmlSerializer, but not
a TimeSpan?

Afraid can't help with the actual problem, but you can circumvent it by
using long. TimeSpan uses it for ticks so you get the value back without too
much hassle.
 
T

The Last Danish Pastry

Since you've asked this, what, 3 times - I think we can assume that
nobody has a satisfactory answer.

Yes it is a known issue; it has been there a while. You could log a
bug on connect.microsoft.com to see if anybody there says anything
useful, or you could work around it - by either exposing this as a
DateTime (only using the TimeOfDay part), an integer (the minutes /
seconds / whatever resolution you need) or as a string.

Not great, but that is how it is at time moment via XmlSerializer.

Thank you for that - and I sincerely apologise for the multiple
postings.

As it happens, I am not really interested in serializing a TimeSpan
struct. Rather I am trying to write a serialize utility class (which
uses XmlSerializer as the underlying serializer).

One method which I would like to write is a "Can you serialize that
object?" method.

I have made some progress with the DateTime/TimeSpan question...

Using reflection I can find out information about those two structs.

In particular, the interfaces which they implement...

The 6 interfaces of DateTime:
System.IComparable
System.IFormattable
System.IConvertible
System.Runtime.Serialization.ISerializable
System.IComparable`1[System.DateTime]
System.IEquatable`1[System.DateTime]

The 3 interfaces of TimeSpan:
System.IComparable
System.IComparable`1[System.TimeSpan]
System.IEquatable`1[System.TimeSpan]

So, TimeSpan does not implement
System.Runtime.Serialization.ISerializable, despite having

IsSerializable=true

and

Attributes={Public, SequentialLayout, Sealed, Serializable,
BeforeFieldInit}

So, maybe I can yet write my "Can you serialize that object?" method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top