Overriding datetime parsing in XmlSerializer without changing generated code



I am accessing a third party API and retrieving some XML. I was also able to access the xsd for it and I use the xsd to generate objects for deserialization using XmlSerializer.


The XML contains dates like this:



<modified>2014-08-19T06:39:13.269-0400</modified>


The generated code looks like this:



[System.Xml.Serialization.XmlElementAttribute(DataType="time")]
public System.DateTime modified {
get {
return this.modifiedField;
}
set {
this.modifiedField = value;
}
}


Unfortunately, this results in errors when attempting to deserialize.


I can work around this by marking it with a XmlIgnore attribute and adding code like this to a partial class:



[XmlElement("modified")]
public string modifiedAsString
{
get { return this.modified.ToString(); }
set { this.modified = System.DateTime.Parse(value); }
}


However, the ignore part is a change to the generated classes and I'd have to make hundreds of these changes to account for all of the datetimes.


I tried the MetadataType thing to apply the attribute to it from the outside but unfortunately XmlSerializer doesn't pick that up.


Does anyone have a suggestion on how I can parse this without changing the generated code or the input XML (which is out of my control)?


No comments:

Post a Comment