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

  • SEARCH
  • Home
  • 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

I'm trying to serialize an object to XML that has a number of properties,
I am trying to serialize a C# object to JSON using JSON.net library. The
I have an custom object I'm trying to serialize using Protobuf-net and the following
I'm trying to serialize an immutable message that has an immutable base class. However,
I am trying to serialize a .net object contains another data contract object as
Ran into an Out of Stack Space error trying to serialize an ASP.Net AJAX
I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to
I'm trying to use Json.NET to serialize and deserialze a tree structure for jsTree.
I have been trying to serialize a list that contains arrays and lists. I
I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like

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.