Is there any way I can define how a DateTime should be serialized/deserialized using something similar to the XmlAttribute tag? In the past I would make the field a string and then do something like this in the constructor:
this.DateField = XmlConvert.ToString(passedObject.Date, XmlDateTimeSerializationMode.Utc);
However, I’d like to actually have the field be a DateTime and somehow tag that it should be serialized as UTC:
[System.Xml.Serialization.XmlAttribute()] // XmlDateTimeSerializationMode tag here?
public DateTime DateField;
How would I do that?
I’m not sure you can do it via attributes, as your crossing the line from serializing your data as it is, to transforming it and then serializing.
You could perhaps get the same result by changing the way you represent the data, by adding a UTC protected property, i.e: