System. TimeSpan

I

Ivan A.

Hi!


Why I can't serialize TimeSpan structure with XmlSerializer? This is what I
do:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public TimeSpan span = new TimeSpan(1, 2, 3);
public DateTime time = new DateTime(1, 1, 1, 1, 1, 1, 1);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(Class1));

XmlTextWriter xmlwriter = new XmlTextWriter("c:\\testser.xml",
System.Text.Encoding.Unicode);

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Serialize(xmlwriter, c);

xmlwriter.Close();

}
}
}


And this is what I get::

<?xml version="1.0" encoding="utf-16" ?>
- <Class1 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<span />
<time>0001-01-01T01:01:01.0010000+02:00</time>
</Class1>
 
B

Ben Lucas

Ivan,

It looks like the TimeSpan structure doesn't serialize as expected with the
XmlSerializer. However, there is a workaround. If you tell the serializer
to ignore the TimeSpan and include a property that provides access to the
ticks off the TimeSpan, you can serialize that. Look at the following
modification to your code:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
[XmlIgnore]
public TimeSpan span = new TimeSpan(1, 2, 3);
public DateTime time = new DateTime(1, 1, 1, 1, 1, 1, 1);

public long elapsedTicks
{
get { return span.Ticks; }
set { span = new TimeSpan(value); }
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(Class1));

XmlTextWriter xmlwriter = new XmlTextWriter("c:\\testser.xml",
System.Text.Encoding.Unicode);

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Serialize(xmlwriter, c);
xmlwriter.Flush();
xmlwriter.Close();

}
}
}

Using this, I had the following results from the program:

<?xml version="1.0" encoding="utf-16" ?>
- <Class1 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<time>0001-01-01T01:01:01.0010000-08:00</time>
<elapsedTicks>109210000000</elapsedTicks>
</Class1>


Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
 
I

Ivan A. Gavrilyuk

Thanks, I will use this! Looks like this is bug in FCL.

By the way can anyone explain me why .NET serializers store default property
values? Isn't it superflorous?

Ben Lucas said:
Ivan,

It looks like the TimeSpan structure doesn't serialize as expected with the
XmlSerializer. However, there is a workaround. If you tell the serializer
to ignore the TimeSpan and include a property that provides access to the
ticks off the TimeSpan, you can serialize that. Look at the following
modification to your code:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
[XmlIgnore]
public TimeSpan span = new TimeSpan(1, 2, 3);
public DateTime time = new DateTime(1, 1, 1, 1, 1, 1, 1);

public long elapsedTicks
{
get { return span.Ticks; }
set { span = new TimeSpan(value); }
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(Class1));

XmlTextWriter xmlwriter = new XmlTextWriter("c:\\testser.xml",
System.Text.Encoding.Unicode);

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Serialize(xmlwriter, c);
xmlwriter.Flush();
xmlwriter.Close();

}
}
}

Using this, I had the following results from the program:

<?xml version="1.0" encoding="utf-16" ?>
- <Class1 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<time>0001-01-01T01:01:01.0010000-08:00</time>
<elapsedTicks>109210000000</elapsedTicks>
</Class1>


Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

Ivan A. said:
Hi!


Why I can't serialize TimeSpan structure with XmlSerializer? This is
what
I
do:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public TimeSpan span = new TimeSpan(1, 2, 3);
public DateTime time = new DateTime(1, 1, 1, 1, 1, 1, 1);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(Class1));

XmlTextWriter xmlwriter = new XmlTextWriter("c:\\testser.xml",
System.Text.Encoding.Unicode);

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Serialize(xmlwriter, c);

xmlwriter.Close();

}
}
}


And this is what I get::

<?xml version="1.0" encoding="utf-16" ?>
- <Class1 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<span />
<time>0001-01-01T01:01:01.0010000+02:00</time>
</Class1>
 

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