By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

System. TimeSpan

P: n/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>
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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." <iv************@hotmail.com> wrote in message
news:uZ**************@TK2MSFTNGP11.phx.gbl...
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>

Nov 16 '05 #2

P: n/a
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" <be*@nospam.solien.nospam.com> wrote in message
news:wd********************@comcast.com...
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." <iv************@hotmail.com> wrote in message
news:uZ**************@TK2MSFTNGP11.phx.gbl...
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>


Nov 16 '05 #3

P: n/a
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" <be*@nospam.solien.nospam.com> wrote in message
news:wd********************@comcast.com...
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." <iv************@hotmail.com> wrote in message
news:uZ**************@TK2MSFTNGP11.phx.gbl...
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>


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.