问题描述
我遇到了一个问题,我正在将一个 DataSet 写入 XML,其中包含一个 DateTime 类型的列,并且我想控制输出格式.
I've got an issue where I am writing a DataSet to XML that has a column of type DateTime and I want to control the output format.
DataSet data = LoadDataSet();
data.Tables[0].Rows[0]["MyDate"] = DateTime.Now;
data.WriteXml(myFile);
默认情况下,XML 中 DateTime 的格式如下:
By default the format of the DateTime in the XML seems to be as follows:
2011-08-02T17:39:00-07:00
我想使用自定义日期格式,或者至少去掉时区信息.
I'd like use a custom date format, or at the very least strip the timezone info.
有什么方法可以控制我的数据集 XML 中 DateTime 列的格式?
Is there any way to control the format of the DateTime columns in my dataset XML?
我的直觉说不,因为我假设这样做是为了促进跨时区的数据转换,但我注意到即使 DateTime 列标签省略了时区数据,我也可以成功读取 DataSet XML,所以我希望我在写入 XML 时可以做类似的事情.
My gut says no since I am assuming it is done this way to facilitate conversion of data across time zones but I've noticed that I can successfully read DataSet XML even if DateTime column tags omit the timezone data so I was hoping I can do something analogous when writing to XML.
推荐答案
DateTime
在 XML 中有一种标准格式.这就是 WriteXml
将使用的格式.如果您需要不同的格式,则不需要使用 DateTime
.正如其他人所说,请改用 String
.
There is one standard format for DateTime
in XML. That's the format that WriteXml
will use. If you need a different format, then you need to not use DateTime
. As others have said, use String
instead.
这篇关于在 .NET 中使用 DataSet.WriteXml 时的自定义日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!