Using "FOR XML" with XML Serialization with DATETIME type
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],
开发者_JS百科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()
?
I don't see a closing tag for < MyObject > ?
Appears to "just work" with SQL Server 2008, where as we'd been using SQL Server 2005 beforehand
精彩评论