Custom DateTime formats when using DataSet.WriteXml in .NET(在 .NET 中使用 DataSet.WriteXml 时的自定义日期时间格式)
问题描述
我遇到了一个问题,我正在将一个 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 时的自定义日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 .NET 中使用 DataSet.WriteXml 时的自定义日期时间格式
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
