How to prevent DateTime from being included in zone offset in xsd: dateTime SOAP element?

I have this in some kind of WSDL:

<element name="startDate" type="xsd:dateTime"/> <element name="endDate" type="xsd:dateTime"/> 

The result is the following text in a SOAP envelope:

 <startDate>2008-10-29T12:01:05</endDate> <endDate>2008-10-29T12:38:59.65625-04:00</endDate> 

Only a few times have milliseconds and zone offset. This causes me a headache because in this example I am trying to get a range of 37 minutes and 54 seconds, but due to the bias I get 4 hours 37 minutes 54.65625 seconds. Is this some kind of rounding error in DateTime? How to prevent this?

+6
soap datetime
source share
2 answers

I suspect your endDate value has a Kind property set to DateTimeKind.Local.

You can change this to DateTimeKind.Unspecified as follows:

 endDate = DateTime.SpecifyKind(endDate, DateTimeKind.Unspecified) 

after which I believe it will be serialized without a timezone offset.

Note that you will get a DateTime with DateTimeKind.Local if you initialized it with DateTime.Now or DateTime.Today and DateTimeKind.Utc if you initialized it with Datetime.UtcNow.

+4
source share

What do you use to create a date? If you build this XML in your code rather than using any serializer (WCF or XmlSerializer), you can use System.Xml.XmlConvert to generate and interpret the date as follows:

To create a string placed in XML:

 DateTime startDate = DateTime.Now; string startDateString = System.Xml.XmlConvert.ToString(startDate); 

To get a date from XML:

 DateTime startDateFromXml = System.Xml.XmlConvert.ToDateTime(startDateString); 

If you start with two DateTime instances that differ by 37 minutes and 54 seconds before you paste them into XML, they will still differ by 37 minutes and 54 seconds after you pull them out of XML.

+1
source share

All Articles