I'm having issues trying to add some dates to a pre-existing class that is loaded via XML Serialisation, and it's not doing what I thought it should do.
I knocked up a basic test with SQL of (where EffectiveFrom and EffectiveTo are declared as DATETIME
)
SELECT o.EffectiveFrom AS [@EffectiveFrom],
o.EffectiveTo AS [@EffectiveTo],
FROM dbo.MyObject o
FOR XML PATH('MyObject'), ROOT('ArrayOfMyObject'), type
Which gives XML:
<ArrayOfMyObject>
<MyObject EffectiveFrom="1977-11-23T00:00:00" EffectiveTo="2050-01-01T00:00:00" />
</ArrayOfMyObject>
Then I've declared the class as:
public class MyObject
{
[XmlAttribute("EffectiveFrom")]
public DateTime EffectiveFrom { get; set; }
[XmlAttribute("EffectiveTo")]
public DateTime EffectiveTo { get; set; }
}
However, the properties aren't being set. I might just be having "a thick day" and missing the blinding obvious, but I would've expected this to "just work" -- any ideas why it isn't?
Will I realy have to create some string properties that call Date.ParseExact()
on the set, and ToString()
on the get, flagging that for XML Serialisation, and flagging the real properties as XmlIgnore()
?