.net XML 文件中日期/时间的正确格式是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/254753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:29:14  来源:igfitidea点击:

What is the correct format to use for Date/Time in an XML file

.netxmldatetime

提问by Jon B

What format do I use for Date/Time when writing to an XML file using .NET? Do I simply use DateTime.ToString(), or do I have to use a specific format?

使用 .NET 写入 XML 文件时,日期/时间使用什么格式?我是简单地使用DateTime.ToString(),还是必须使用特定的格式?

回答by Ryan

I always use the ISO 8601format (e.g. 2008-10-31T15:07:38.6875000-05:00) -- date.ToString("o"). It is the XSD date formatas well. That is the preferred format and a Standard Date and Time Format string, although you can use a manual format string if necessary if you don't want the 'T' between the date and time: date.ToString("yyyy-MM-dd HH:mm:ss");

我总是使用ISO 8601格式(例如2008-10-31T15:07:38.6875000-05:00)- date.ToString("o")。它也是XSD 日期格式。这是首选格式和标准日期和时间格式字符串,但如果您不想在日期和时间之间使用“T”,则可以在必要时使用手动格式字符串:date.ToString("yyyy-MM-dd HH:mm:ss");

EDIT: If you are using a generated class from an XSD or Web Service, you can just assign the DateTime instance directly to the class property. If you are writing XML text, then use the above.

编辑:如果您使用从 XSD 或 Web 服务生成的类,您可以直接将 DateTime 实例分配给类属性。如果您正在编写 XML 文本,请使用上述内容。

回答by jonnii

EDIT: This is bad advice. Use "o", as above. "s" does the wrong thing.

编辑:这是不好的建议。使用“o”,如上。“s”做错了事

I always use this:

我总是用这个:

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

This is correct if your schema looks like this:

如果您的架构如下所示,这是正确的:

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

Which would result in:

这将导致:

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

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

您可以在此处获取更多信息:http: //www.w3schools.com/xml/schema_dtypes_date.asp

回答by chilltemp

If you are manually assembling the XML string use var.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"));That will output the official XML Date Time format. But you don't have to worry about format if you use the built-in serialization methods.

如果您手动组装 XML 字符串,请使用var.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"));That 将输出官方 XML 日期时间格式。但是如果您使用内置的序列化方法,则不必担心格式。

回答by Tanktalus

What does the DTD have to say?

DTD 有什么要说的?

If the XML file is for communicating with other existing software (e.g., SOAP), then check that software for what it expects.

如果 XML 文件用于与其他现有软件(例如 SOAP)通信,则检查该软件是否符合预期。

If the XML file is for serialisation or communication with non-existing software (e.g., the one you're writing), you can define it. In which case, I'd suggest something that is both easy to parse in your language(s) of choice, and easy to read for humans. e.g., if your language (whether VB.NET or C#.NET or whatever) allows you to parse ISO dates (YYYY-MM-DD) easily, that's the one I'd suggest.

如果 XML 文件用于序列化或与不存在的软件(例如,您正在编写的软件)通信,您可以定义它。在这种情况下,我会建议一些既易于用您选择的语言解析又易于人类阅读的内容。例如,如果您的语言(无论是 VB.NET 还是 C#.NET 或其他语言)允许您轻松解析 ISO 日期 (YYYY-MM-DD),那么这就是我建议的语言。

回答by Starnuto di topo

The XmlConvertclass provides these kinds of facilities. About DateTimes, in particular, be careful about obsolete methods. See also: https://stackoverflow.com/a/7457718/1288109

XmlConvert类提供这类设施。关于 DateTimes,尤其要注意过时的方法。另见:https: //stackoverflow.com/a/7457718/1288109