开发者

XmlSerializer.Deserialize method appends timezone to a time and datetime field

开发者 https://www.devze.com 2022-12-24 23:43 出处:网络
Have a small script in Microsoft.NET 2.0 that deserializes a XML back to a typed object, connects dyanimcally to a web service using ServiceDescription and binds the deserialized typed object to the W

Have a small script in Microsoft.NET 2.0 that deserializes a XML back to a typed object, connects dyanimcally to a web service using ServiceDescription and binds the deserialized typed object to the WebMethod inbound. The XML prior to serialization looks like below

<completion_time>12:19:38</completion_time> 

on th开发者_如何学编程e wire when communicating to the web service looks like below

<completion_time>12:19:38.0000000-04:00</completion_time>

with the timezone appended to the end. This is causing the time to be read differently when communicating to a web service at a different timezone. is there anyway to let XmlSerializer skip the timezone? Or any other known workarounds?


Ran into the same issue. Finally found the DateTimeMode property on the DataColumn object. It takes a member of the DataSetDateTime enumeration which controls how the Time is serialized. Setting it to Unspecified will prevent the serializer from doing any conversion of the time.

You can loop through an existing DataSet and set the mode like the following:

        foreach (DataColumn column in table.Columns) {
            if (column.DataType == typeof(DateTime)) {
                column.DateTimeMode = DataSetDateTime.Unspecified;
            }   
        }


You can change the type of 'completion_time' field from DateTime to string and use datetime formater to get datetime string without timezone

DateTime date = DateTime.UtcNow; date.ToString("hh:mm:ss");


http://blogs.msdn.com/brada/archive/2004/04/13/112784.aspx

[XmlElementAttribute(DataType="date")]

public DateTime date;

or

[XmlAttributeAttribute(DataType="date")]

public DateTime date;
0

精彩评论

暂无评论...
验证码 换一张
取 消