I'm updating an application that reads from an XML file using System.Xml.XmlReader and needs to read a value that's an Xml date-time and generate a string representation of that as local time, without time zone info.
The previous code just pulled the attribute value string apart at fixed positions and then re-constructed a string with string.Format(). I thought this was a bit fragile (and very ugly).
I changed it to:
DateTime runDate = reader.ReadElementContentAsDateTime();
string output = runDate.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
On my system (US PDT), this
seemed to work perfectly:
The xml date time string: 2015-02-24T10:21:42.3414491-08:00
Gave back: 02/24/2015 10:21:42
However: This fails the unit tests
when they are run on the integration build server, because that server is in a
different time zone.
So, is there a way to parse a string into a local DateTime ignoring the time zone, preferably without string manipulation.
I know that I could use a Regex but it just seems wrong not to use DateTime as the intermediate form for data which
is a date time.
---
This is quite frustrating, because the ReadElementContentAsDateTime solution
does work in the normal case of converting the xml immediately after it is generated, on the same system. It's just the build server unit tests that fail.
What I have tried:
I tried ReadElementContentAsString and then using XmlConvert.ToDateTime with the various XmlDateTimeSerializationMode values.
No luck.
There just doesn't seem to be a way to parse
ignoring the timezone info.