当时间具有时区信息时,将XML读入Datatable会给出错误的DateTime [英] Reading XML into Datatable gives incorrect DateTime when the time has Time Zone info
问题描述
因此,我的客户端运行一些代码,将其当前时间写入xml文件,然后我想将其回读到数据表中,但是我得到的时间信息不正确.
So my client runs some code that writes their current time to an xml file which I then want to read back into a data table but I am getting incorrect time information.
例如,他们的当前时间是 09:31 ,其时区是 UTC + 1:00 .
For example their current time is 09:31 their time zone is UTC+1:00.
我的代码是:
var ds = new DataSet("MyDataSet");
var dt = ds.Tables.Add("MyDataTable");
dt.Columns.Add("MyDateTime", typeof(DateTime));
var startingDateTime = DateTime.Now;
dt.Rows.Add(startingDateTime);
String xmlDT = String.Empty;
using (MemoryStream memoryStream = new MemoryStream())
{
dt.WriteXml(memoryStream,XmlWriteMode.WriteSchema);
xmlDT = Encoding.UTF8.GetString(memoryStream.ToArray());
}
string myFile = @"C:\Users\me\Documents\test1.txt"
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlDT);
doc.Save(myFile);
myFile现在包含:
myFile now contains:
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="MyDataTable" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="MyDataTable">
<xs:complexType>
<xs:sequence>
<xs:element name="MyDateTime" type="xs:dateTime" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<MyDataTable>
<MyDateTime>2015-02-12T09:31:37.4250365+01:00</MyDateTime>
</MyDataTable>
</NewDataSet>
此文件然后发送到我在英国 (+ 00:00) 的服务器中,然后我使用以下命令读取文件:
This file is then sent to my server in the UK (+00:00) where I then read the file using:
DataTable datatable2 = new DataTable();
datatable2.ReadXml(myFile);
并且我的数据表现在包含1行,其日期时间为 08:30 ,但这是不正确的,我希望它存储客户时间.更改客户端或服务器代码后,我该怎么做?
and my datatable now contains 1 row that has a datetime of 08:30 but this is incorrect and I would like it to store the clients time. How would I do this with a change to either my client or server code?
推荐答案
因此您可以在客户端添加以下行:
so client side you can add the line:
dt.Columns[0].DateTimeMode = DataSetDateTime.Unspecified;
或作为更通用的方法:
public static void RemoveTimezoneForDataSet(DataSet ds)
{
foreach (DataTable dt in ds.Tables)
{
foreach (DataColumn dc in dt.Columns)
{
if (dc.DataType == typeof(DateTime))
{
dc.DateTimeMode = DataSetDateTime.Unspecified;
}
}
}
}
这会将XML保存为:
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="MyDataTable" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="MyDataTable">
<xs:complexType>
<xs:sequence>
<xs:element name="MyDateTime" msdata:DateTimeMode="Unspecified" type="xs:dateTime" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<MyDataTable>
<MyDateTime>2015-02-12T09:13:39.8180356</MyDateTime>
</MyDataTable>
</NewDataSet>
因此时间可以在服务器上正确序列化
So the time gets serialized correctly on the server
这篇关于当时间具有时区信息时,将XML读入Datatable会给出错误的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!