The DateTimeOffset property I have in this class doesn’t get rendered when the data is represented as Xml. What do I need to do to tell the Xml serialization to render that proper as a DateTime or DateTimeOffset?
[XmlRoot("playersConnected")]
public class PlayersConnectedViewData
{
[XmlElement("playerConnected")]
public PlayersConnectedItem[] playersConnected { get; set; }
}
[XmlRoot("playersConnected")]
public class PlayersConnectedItem
{
public string name { get; set; }
public DateTimeOffset connectedOn { get; set; } // <-- This property fails.
public string server { get; set; }
public string gameType { get; set; }
}
and some sample data…
<?xml version="1.0" encoding="utf-8"?>
<playersConnected
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<playerConnected>
<name>jollyroger1000</name>
<connectedOn />
<server>log1</server>
<gameType>Battlefield 2</gameType>
</playerConnected>
</playersConnected>
Update
I’m hoping there might be a way through an Attribute which I can decorate on the property…
Bonus Question
Any way to get rid of those two namespaces declared in the root node? Should I?
I’ve ended up just doing this…
Added the two extension methods …
Modified the ViewData class…
Changed how I set the viewdata properties…
Done. Not sure if that’s the best way .. but now that viewdata property has the same values for either Json or Xml.