G33kKahuna
G33kKahuna

Reputation: 1810

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

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 the 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?

Upvotes: 0

Views: 1512

Answers (3)

Brad Patton
Brad Patton

Reputation: 4205

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

Upvotes: 1

user117499
user117499

Reputation:

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

[XmlElementAttribute(DataType="date")]

public DateTime date;

or

[XmlAttributeAttribute(DataType="date")]

public DateTime date;

Upvotes: 0

petkov_d
petkov_d

Reputation: 133

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

Upvotes: 0

Related Questions