locked
String was not recognized as a valid DateTime. XML serialization with xs:datetime

    Question

  • Hi, i've got a strange thing with serialization. If serialize class RequestClearingType on its own, the date in element "ClearingDate" is represented in correct format:<RequestClearingType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><ClearingDate xmlns="http://www.intercard.de/XMLSchema/eGateway.xsd">2007-01-25T13:20:31.0445976+01:00</ClearingDate></RequestClearingType>If serialize class eGateway, the element "ClearingDate" is losing its time information.<eGateway xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.intercard.de/XMLSchema/eGateway.xsd"><Request><Header><MerchantId>990002</MerchantId><TransactionId>f24d7f9cd4594852ae0491a1ce8b4e47</TransactionId><TerminalId xsi:nil="true"></TerminalId><Transaction><Clearing><ClearingDate>2007-01-25</ClearingDate></Clearing></Transaction></Header></Request>Here is the code fragment of my xsd file:<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.intercard.de/XMLSchema/eGateway.xsd" targetnamespace="http://www.intercard.de/XMLSchema/eGateway.xsd" elementformdefault="qualified">...<xs:complexType name="RequestClearingType"><xs:all><xs:element name="ClearingDate" type="xs:dateTime" nillable="true" minoccurs="0"> </xs:element></xs:all>......</xs:complexType><xs:element name="eGateway"><xs:annotation><xs:documentation>eGateway transaction</xs:documentation></xs:annotation> <xs:complexType><xs:choice><xs:element name="Request"><xs:annotation><xs:documentation>Transaction request data</xs:documentation></xs:annotation><xs:complexType><xs:all><xs:element ref="Header"><xs:element name="Transaction"><xs:annotation><xs:documentation>transaction data</xs:documentation></xs:annotation><xs:complexType><xs:choice><xs:element name="Clearing" type="RequestClearingType"></xs:element></xs:choice></xs:complexType> </xs:element></xs:element></xs:all> </xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema></eGateway>If i try to deserialize a string containg the 2007-01-25T13:20:31.0445976+01:00 as date i get following error: System.InvalidOperationException : There is an error in XML document (1, 403).  ----> System.FormatException : String was not recognized as a valid DateTime.--InvalidOperationException   at System.DateTimeParse.ParseExactMultiple(String s, String[] formats, DateTimeFormatInfo dtfi, DateTimeStyles style)   at System.DateTime.ParseExact(String s, String[] formats, IFormatProvider provider, DateTimeStyles style)   at System.Xml.XmlConvert.ToDateTime(String s, String[] formats)   at System.Xml.Serialization.XmlCustomFormatter.ToDateTime(String value, String[] formats)   at System.Xml.Serialization.XmlCustomFormatter.ToDate(String value)   at System.Xml.Serialization.XmlSerializationReader.ToDate(String value)   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadereGateway.Read8_NullableOfDateTime(Boolean checkType)   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadereGateway.Read49_RequestClearingType(Boolean isNullable, Boolean checkType)   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadereGateway.Read57_eGatewayRequestTransaction(Boolean isNullable, Boolean checkType)   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadereGateway.Read58_eGatewayRequest(Boolean isNullable, Boolean checkType)   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadereGateway.Read59_eGateway(Boolean isNullable, Boolean checkType)   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadereGateway.Read60_eGateway()
    Thursday, February 01, 2007 12:51 PM