views:

110

answers:

2

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

A: 

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

Preets
why -1 ! I did not see the < ... /> closing slash when I posted this !
Preets
and that is what Rowland changed in the Edit ! Jesus !
Preets
The -1 is probably as this *isn't an answer*; you could've added your comment as a comment, after all...
Rowland Shaw
oh my ! so leave it at zero id say ! Its not military rule in here is it ! I tried the example in C# and it worked perfectly for me ! Except that it complained about the missing closing tag ! So, it 'could' have solved the issue, but it didn't in this case. No ans on SO can be **guaranteed** to solve a problem !
Preets
@Preets: If it doesnt solve the problem, then it gets downvoted. That's kind of the point of the site.
Chris Lively
Hmm.. well I suppose that is a fair point of view. For me it was +1 = Useful / 0 = Not Useful / -1 = Totally Wrong. I shall be more careful with my replies ;-)
Preets
A: 

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

Rowland Shaw