Can you specify a format for the XmlSerialization datetime?

I need to serialize / deserialize datetime in yyyyMMdd format for an XML file. Is there an attribute / workaround that I can use for this?

+51
c # xml serialization
Jul 13 '09 at 10:59
source share
2 answers

No no. If it is in this format, then this is not a valid dateTime value with respect to the XML schema.

The best you can do is the following:

[XmlIgnore] public DateTime DoNotSerialize {get;set;} public string ProxyDateTime { get {return DoNotSerialize.ToString("yyyyMMdd");} set {DoNotSerialize = DateTime.Parse(value);} } 
+62
Jul 13 '09 at 11:04
source share
— -

XmlElementAttribute #DataType should provide what you need:

 [XmlElement(DataType="date")] public DateTime Date1 {get;set;} 

This will cause the Date1 property to be serialized according to the xml date format.

+18
Feb 17 2018-11-11T00:
source share



All Articles