What is the correct format for date / time in an XML file

What format do I use for date / time when writing to an XML file using .NET? Am I just using DateTime.ToString() , or do I need to use a specific format?

+84
xml datetime
Oct 31 '08 at 19:58
source share
5 answers

I always use the ISO 8601 format (for example, 2008-10-31T15:07:38.6875000-05:00 ) - date.ToString("o") . This is the XSD date format . This is the preferred format and Standard date and time string , although you can use the format string manually if necessary if you do not want a “T” between the date and time: date.ToString("yyyy-MM-dd HH:mm:ss");

EDIT: If you are using a generated class from XSD or a web service, you can simply assign the DateTime instance directly to the class property. If you are writing XML text, use the above.

+118
Oct 31 '08 at 20:13
source share

EDITOR: This is bad advice. Use "o" as above. "s" is doing the wrong thing.

I always use this:

 dateTime.ToUniversalTime().ToString("s"); 

This is correct if your circuit looks like this:

 <xs:element name="startdate" type="xs:dateTime"/> 

This will lead to:

 <startdate>2002-05-30T09:00:00</startdate> 

You can get more information here: http://www.w3schools.com/xml/schema_dtypes_date.asp

+38
Oct 31 '08 at 20:02
source share

If you manually compiled the XML string, use var.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ")); which will output the official XML date format. But you do not need to worry about the format if you use the built-in serialization methods.

+4
Oct 31 '08 at 20:06
source share

What does the DTD say?

If the XML file is designed to communicate with other existing software (for example, SOAP), then check this software for what it expects.

If the XML file is intended for serialization or communication with non-existent software (for example, the one you are writing), you can define it. In this case, I suggest that it is easy to analyze in your chosen language (s) and is easy to read for people. for example, if your language (be it VB.NET or C # .NET or something else) makes it easy to parse ISO dates (YYYY-MM-DD), which I would suggest.

+3
Oct. 31 '08 at 20:01
source share

The XmlConvert class provides these features. For DateTimes in particular, be careful with deprecated methods. See also: https://stackoverflow.com>

0
Aug 28 '14 at 8:05
source share



All Articles