Reading XML into Datatable gives incorrect DateTime when the time has Time Zone info



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.


For example their current time is 09:31 their time zone is UTC+1:00 .


my code is:



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 now contains:



<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://ift.tt/tphNwY" 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>


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);


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?


No comments:

Post a Comment