문제

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()?

도움이 되었습니까?

해결책 2

Appears to "just work" with SQL Server 2008, where as we'd been using SQL Server 2005 beforehand

다른 팁

I don't see a closing tag for < MyObject > ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top