tags:

views:

3180

answers:

4

Obviously the reader doesn't like this format incoming from the response XML.

Wondering if I can reformat this. Trying to convert to DateTime using the following code with my XmlReader:

reader.ReadContentAsDateTime();
+5  A: 

Xml readers generally expect dates/times in a very specific format; you can use this yourself using XmlConvert:

string s = XmlConvert.ToString(DateTime.Now);
DateTime when = XmlConvert.ToDateTime(s);

If you are using something else, you'll have to read it as a string and use DateTime.TryParseExact (or similar) to specify the actual format string:

string s = reader.ReadContentAsString();
DateTime when = DateTime.ParseExact(s, "M/d/yy hh:mm tt",
     CultureInfo.InvariantCulture);

If you are using XmlSerializer, you could use a shim property to do the conversion - let me know if this is what you are doing...

Marc Gravell
+1 For the example.
Andrew Hare
+3  A: 

According to the XML schema spec, date time values should be in ISO8601 format, e.g., something like

2009-03-13T22:16:00
David Norman
For the example given: 2009-03-18T22:16:00
Marc Gravell
Updated. Thanks.
David Norman
A: 

If you want a more flexible date/time parser, my company is looking into selling this library that would parse that just fine.

BCS
A: 

Hi, I am tryign to integrate my application to a webservice. The XML Soap requires 'cccc-yy-dd' as the date parameter. If I try to give anything else it throws an error. While on the java side the same code accepts a type calendar for the birthday.

can anyone help me .. how can i achieve this ?

AHK