It looks like the formats you're dealing with are in the standard XML DateTime format. So in this case you don't need a proxy string property to handle it, you can use the built in functionality of the XmlSerializer and specify the dateTime data type on the relevant XmlElement. Update: specifying the dateTime doesn't work if the element is empty (even in combination with isNullable), here is a way to convert the string manually to a DateTime.
For example..
[XmlRoot("person")]
public class Person
{
[XmlElement("last-name")]
public string LastName { get; set; }
[XmlElement("first-name")]
public string FirstName { get; set; }
[XmlElement("id")]
public int Id { get; set; }
[XmlElement(ElementName = "last-changed-on")]
public XmlDateTime LastChangedOn { get; set; }
}
Edit: sounds like you have Xml where LastChangedOn can be empty or missing. In this case you can implement a custom class to handle the conversion and keep the Person class clean. I've added implict conversions so you can just treat the property as if it were a DateTime. Above changes slightly to have XmlDateTime instead of DateTime.
[DebuggerDisplay("{Value}")]
public class XmlDateTime : IXmlSerializable
{
public DateTime Value { get; set; }
public bool HasValue { get { return Value != DateTime.MinValue; } }
private const string XML_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
if (reader.IsEmptyElement)
{
reader.ReadStartElement();
return;
}
string someDate = reader.ReadInnerXml();
if (String.IsNullOrWhiteSpace(someDate) == false)
{
Value = XmlConvert.ToDateTime(someDate, XML_DATE_FORMAT);
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
if (Value == DateTime.MinValue)
return;
writer.WriteRaw(XmlConvert.ToString(Value, XML_DATE_FORMAT));
}
public static implicit operator DateTime(XmlDateTime custom)
{
return custom.Value;
}
public static implicit operator XmlDateTime(DateTime custom)
{
return new XmlDateTime() { Value = custom };
}
}