Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 144521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:15:59+00:00 2026-05-11T08:15:59+00:00

I am trying to serialize a .NET TimeSpan object to XML and it is

  • 0

I am trying to serialize a .NET TimeSpan object to XML and it is not working. A quick google has suggested that while TimeSpan is serializable, the XmlCustomFormatter does not provide methods to convert TimeSpan objects to and from XML.

One suggested approach was to ignore the TimeSpan for serialization, and instead serialize the result of TimeSpan.Ticks (and use new TimeSpan(ticks) for deserialization). An example of this follows:

[Serializable] public class MyClass {     // Local Variable     private TimeSpan m_TimeSinceLastEvent;      // Public Property - XmlIgnore as it doesn't serialize anyway     [XmlIgnore]     public TimeSpan TimeSinceLastEvent     {         get { return m_TimeSinceLastEvent; }         set { m_TimeSinceLastEvent = value; }     }      // Pretend property for serialization     [XmlElement('TimeSinceLastEvent')]     public long TimeSinceLastEventTicks     {         get { return m_TimeSinceLastEvent.Ticks; }         set { m_TimeSinceLastEvent = new TimeSpan(value); }     } } 

While this appears to work in my brief testing – is this the best way to achieve this?

Is there a better way to serialize a TimeSpan to and from XML?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:16:00+00:00Added an answer on May 11, 2026 at 8:16 am

    The way you’ve already posted is probably the cleanest. If you don’t like the extra property, you could implement IXmlSerializable, but then you have to do everything, which largely defeats the point. I’d happily use the approach you’ve posted; it is (for example) efficient (no complex parsing etc), culture independent, unambiguous, and timestamp-type numbers are easily and commonly understood.

    As an aside, I often add:

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] 

    This just hides it in the UI and in referencing dlls, to avoid confusion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Scala 2.8 and liftweb.net 2.0 I'm trying to serialize a case-class to Json
I'm trying to figure out serialization of .net arrays to XML. Here's a piece
I am trying to serialize/deserialize a List as defined below with protobuf-net v2, and
I am trying to serialize a list that contains non-system types. Below is my
I'm trying to serialize an object to meet another systems requirements. It need to
I ran into problems when trying to xml serialize a class to xml after
I'm coding in C# for the .NET Framework 3.5. I am trying to parse
I'm trying to serialize a class in which I have a bitmap variable. Here
I am trying to serialize a Dictionary to JSON, and get the following exception:
I'm trying to understand ReST and XML a little more - A third party

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.