XML DateTime serialization

Hello,

I am upgrading a .Net Framework 1.1 application that uses an XML document and writes this document to a file. One of the fields in the document has the xml datatype dateTime. This dateTime is output in a format containing 7 digits for the fractions of the seconds (yyyy-mm-ddThh:mm:ss.sssssss+timezone). As the dateTime we use is only accurate to the minute, the seconds and fractions of seconds remain zero and are output as zeros.

After compiling the application with the .Net Framework 2.0 this changes. If the fractions of seconds are zero they are no longer written to the file (the seconds are still written as 00). As we exchange this file with a business partner who requires the fractions of the seconds to successfully parse the file the upgrade breaks our application.

Has anyone had the same behaviour and is there a way to change this back to the old way

Thanks

Daniel



Answer this question

XML DateTime serialization

  • Ice Age

    I am having a similar problem using XmlSerializer.
    Does anyone have a workaround other than doing a regex.replace on the serialized string


  • benny353

    We had a similar issue with the change. We change the (yyyy-mm-ddThh:mm:ss.sssssss+timezone) format to SQL format....

    Anyway you could use a regular expression (assuming the second fractions are not important):

    public string ConvertDataSetXml(string xmlInput)

    {

    return System.Text.RegularExpressions.Regex.Replace(xmlInput, @"( <date>\d{2,4}-\d{1,2}-\d{1,2})T( <time>\d{2}:\d{2}:\d{2})\.*\d*( <zone>(-|\+)\d{2}:\d{2})", "${date}T${time}.0000000${zone}");

    }


  • Sgupta

    I am using a DataSet's Write method. I also got the same result with XmlDocument.Save.
  • AdrianGodong

    Actually a better expression is:

    @"( <date>\d{2,4}-\d{1,2}-\d{1,2})T( <time>\d{2}:\d{2}:\d{2})\. \d{0,7}( <zone>(-|\+)\d{2}:\d{2})"


  • Alex1st

    I opened a support call with Microsoft Professional Support on this.

    Does anyone have any ideas for a workaround

    Daniel


  • JessicaM

    Can you provide some details on which .NET APIs exactly you are using to create the XML respectively save it to a file Are you using XmlWriter or XmlSerializer or XmlConvert

  • bryanedds

    I had this problem, converting datetime to ISO 8601, and I found that simply using DateTime.ToString("s") does the trick. This uses DateTimeFormatInfo.SortableDateTimePattern property. Thanks.

    Cale


  • Henry_2003

    Looks like a breaking change in XmlConvert. We'd investigate.

  • XML DateTime serialization