How to serialize date date

Work to get DateTimes for any time zone. I am using DateTimeOffset, a string and an XmlElement attribute. When I do this, I get the following error:

[InvalidOperationException: 'dateTime' is an invalid value for the XmlElementAttribute.DataType property. dateTime cannot be converted to System.String.]
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeat, Logical openModel, RecursionLimiter delimiter) +450

[InvalidOperationException: There is an error reflecting the type 'System.String'.]
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeat, Logical openModel, RecursionLimiter delimiter) +1621
System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping (MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +8750
System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping (StructModel parent, FieldModel model, XmlAttributes a, String ns, Recursion limit constraint) +139
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers (StructMapping map, StructModel model, Boolean openModel, String typeName, Recursion Constraint Limiter) +1273

[InvalidOperationException: There is an error reflecting the 'creationTimeX' property.] ...

code:

[System.Xml.Serialization.XmlElement(ElementName = "creationTime", DataType="dateTime")] public string creationTimeX { get { return this.creationTimeField.ToString("yyyy-MM-ddTHH:mm:sszzz"); } set { DateTimeOffset.TryParse(value, out this.creationTimeField); } } [System.Xml.Serialization.XmlIgnoreAttribute()] public System.DateTimeOffset creationTime { get { return this.creationTimeField; } set { this.creationTimeField = value; } } 
+4
source share
5 answers

Take a look at this StackOverflow question about date serialization and UTC:

Best practices for serializing DateTime in the .NET Framework 3.5 / SQL Server 2008

You do not need to create a special property to serialize.

+2
source

This is what worked for me

 private const string DateTimeOffsetFormatString = "yyyy-MM-ddTHH:mm:sszzz"; private DateTimeOffset eventTimeField; [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)] public string eventTime { get { return eventTimeField.ToString(DateTimeOffsetFormatString); } set { eventTimeField = DateTimeOffset.Parse(value); } } 
+2
source

I would suggest you serialize DateTime as long (this is what the internal implementation uses to store the actual value).

You can use DateTime.Ticks to get the value, and it has a constructor that takes a long time ( Int64 ).

+1
source

Use the XmlConvert.ToDateTimeOffset () and .ToString () methods to correctly serialize and de-serialize DateTimeOffset in the XmlSerializer workaround property.

The full sample in the Microsoft Connect article here and confirmation that, unfortunately, Microsoft will not correct this oversight (it should have been supported initially by XmlSerializer like any primitive type):

https://connect.microsoft.com/VisualStudio/feedback/details/288349/datetimeoffset-is-not-serialized-by-a-xmlserializer

+1
source

David

The property data type (creationTimeX) is a string, and the XmlSerialization data type is "dateTime". That is why you get this exception.

You can fix this by changing the data type to DateTime

Also for your current time question for any time zone, you will need to apply DateTime.Now.ToUniveralTime and apply the corresponding DateTimeFormat template to it.

Steps for them here

http://msdn.microsoft.com/en-us/library/k494fzbf.aspx

Thanks -RVZ

0
source

All Articles