8

I need to know the optimal way of writing/reading DateTime into/from XML. Should I directly write DateTime into XML or DateTime.ToString() into XML?

Second question is how to read the date element from XML. Can casting be used for this? Eg:

(DateTime)rec.Element("Date").value

Or, do I need to parse the string like this? Eg:

DateTime.Parse(rec.Element("Date").value)

3 Answers 3

20

You can use casting of an XElement or XAttribute with LINQ to XML, yes... but not of the string itself. LINQ to XML uses the standard XML format, independent of your culture settings.

Sample:

using System;
using System.Xml.Linq;

class Test
{    
    static void Main()
    {
        DateTime now = DateTime.Now;
        XElement element = new XElement("Now", now);

        Console.WriteLine(element);
        DateTime parsed = (DateTime) element;
        Console.WriteLine(parsed);
    }
}

Output for me:

<Now>2011-01-21T06:24:12.7032222+00:00</Now>
21/01/2011 06:24:12
Sign up to request clarification or add additional context in comments.

18 Comments

@Pankaj: I mean the conversions performed by the LINQ to XML library, as shown in my answer.
@Pankaj: The format isn't part of the value to start with. You should definitely not just call ToString on the DateTime. That will get you XML which depends on the culture of the computer you're running it on. What does "03/05/2011" mean - May 3rd or March 5th? It's ambiguous unless you have the format information as well (which isn't part of the value). The XML format is standardized, and doesn't suffer from this issue.
@Pankaj: Then I suggest you stop doing that :) If you have to update an existing element, you could use r.Element("Date").Value = XmlConvert.ToString(Date, XmlDateTimeSerializationMode.RoundtripKind);
@Pankaj: Then you'd need to specify Date.Value - that's just a normal part of using nullable value types. Note that if you use the XElement constructor instead, that would just happen by magic :)
@Justin: Look at the XElement documentation - it provides a custom explicit conversion. If it didn't, it would fail at compile-time.
|
7

An alternative to @Jon Skeet's answer is to convert the DateTime to a string using the "round trip" format. This converts it to a format that will save and load without losing any information.

string dataToSave = myDateTime.ToString("o");

And convert back again using DateTime.Parse(). The page I've linked to has examples showing you how to convert to/from the string format. All you need to do is store this string into your XML. THis gives you more control over how the data is stored (if you want more control, that is).

2 Comments

More control, but less portability. XML has standardized ways of storing dates and times - I would think very carefully before deliberately straying from them.
@Jon: Yeah, I was trying to point out a more generalised way to serialise to/from text losslessly, but I didn't really put it very well - as you say, I've strayed a bit from the point :-)
5

You can use the XmlConvert class to convert to and from strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.