473,543 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System. TimeSpan

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.Seri alization;

namespace ConsoleApplicat ion1
{
/// <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(t ypeof(Class1));

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

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Seri alize(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.001 0000+02:00</time>
</Class1>
Nov 16 '05 #1
3 13531
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.Seri alization;

namespace ConsoleApplicat ion1
{
/// <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(t ypeof(Class1));

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

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Seri alize(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.001 0000-08:00</time>
<elapsedTicks>1 09210000000</elapsedTicks>
</Class1>
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Ivan A." <iv************ @hotmail.com> wrote in message
news:uZ******** ******@TK2MSFTN GP11.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.Seri alization;

namespace ConsoleApplicat ion1
{
/// <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(t ypeof(Class1));

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

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Seri alize(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.001 0000+02:00</time>
</Class1>

Nov 16 '05 #2
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.sol ien.nospam.com> wrote in message
news:wd******** ************@co mcast.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.Seri alization;

namespace ConsoleApplicat ion1
{
/// <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(t ypeof(Class1));

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

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Seri alize(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.001 0000-08:00</time>
<elapsedTicks>1 09210000000</elapsedTicks>
</Class1>
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Ivan A." <iv************ @hotmail.com> wrote in message
news:uZ******** ******@TK2MSFTN GP11.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.Seri alization;

namespace ConsoleApplicat ion1
{
/// <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(t ypeof(Class1));
XmlTextWriter xmlwriter = new XmlTextWriter(" c:\\testser.xml ", System.Text.Enc oding.Unicode);

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Seri alize(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.001 0000+02:00</time>
</Class1>


Nov 16 '05 #3
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.sol ien.nospam.com> wrote in message
news:wd******** ************@co mcast.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.Seri alization;

namespace ConsoleApplicat ion1
{
/// <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(t ypeof(Class1));

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

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Seri alize(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.001 0000-08:00</time>
<elapsedTicks>1 09210000000</elapsedTicks>
</Class1>
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Ivan A." <iv************ @hotmail.com> wrote in message
news:uZ******** ******@TK2MSFTN GP11.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.Seri alization;

namespace ConsoleApplicat ion1
{
/// <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(t ypeof(Class1));
XmlTextWriter xmlwriter = new XmlTextWriter(" c:\\testser.xml ", System.Text.Enc oding.Unicode);

Class1 c = new Class1();
c.span = new TimeSpan(3, 2, 1);
serializer.Seri alize(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.001 0000+02:00</time>
</Class1>


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1817
by: Doug | last post by:
Is it possible there is a bug with the System.TimeSpan object in .NET? I am using it to track times and am seeing an unusual situation. From what I understand, there are 1000 milliseconds in a second. I have a test I ran using the TimeSpan object which when I use TimeSpan.Milliseconds I got 658 but if I use TimeSpan.Seconds I get 5. If...
5
6572
by: Deiussum | last post by:
I'm running into an issue where I have a timer that appears to be timing out immediately, when it shouldn't be timing out for about int.MaxValue milliseconds. I have written a small app that isolates the problem and was wondering if anyone had any ideas on what I am doing wrong, or if there is some known bug, or I am exceeding some known...
4
4476
by: DWW | last post by:
System.DateTime returns a string with the date and time. I want to convert the time to an integer, for example, convert 10:24:45 to 102445. Much research has turned up a myriad of variations on formatting using Parse and DateTimeFormatInfo, but nothing that returns the time as an integer. Any ideas? Thanks. DW
2
19074
by: DWalker | last post by:
In Visual Studio (Visual Basic) .NET 2002, I noticed that this: Dim Elapsed as DateTime = Now - Now gives a "compile time" error (error in the IDE), saying that the '-' operator is not defined for the Date datatype. On the other hand, Dim Elapsed as DateTime = Now + Now does not give a compile time error but it thinks you're...
10
38597
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
0
1084
by: Arkej | last post by:
Hello everybody! I'm using xsdobjgen tool to generate c# classes. Everything is just fine, but I can't find a way to make a fileld in the generated class of type System.TimeSpan. In the framework deveopers guide I've found xsd:duration should map to System.TimeSpan, but xsdobjgen generates string type for duration. Does anyone have a...
3
2868
by: Rob Meade | last post by:
Hi all, I'm having a bit of trouble with the following function.... Private Function GetSystemUpTime() As TimeSpan ' declare variables Dim Result As TimeSpan Dim PerformanceCounter As PerformanceCounter
9
17742
by: SharpCoderMP | last post by:
i've been experiencing random crashes on some machines running my app. the app never crashed in such way on my dev machine so i'm totally unable to debug this. the error page users get says nothing special: EventType : clr20r3 P1 : app.exe P2 : 1.0.0.0 P3 : 24 P4 : app P5 : 1.0.0.0 P6 : 24 P7 : 11d P8 : 0 P9...
4
2590
by: active T | last post by:
Is it possible to extend the system classes? I want to create a function for converting a DateTime into a TimeSpan. Any Ideas? at the moment i've created: public class Convert {
0
7594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4900
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3395
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
979
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
648
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.