I asked this question yesterday, wondering how to produce XML with attributes using MVCContrib. The answer was to use [XmlAttribute]
.
Since, I've successfully used [XmlAttribute]
to get the following XML:
<?xml version="1.0" encoding="utf-8"?>
<TopTen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SectorName>Property - Direct UK</SectorName>
<PerformanceTo>2011-01-31T00:00:00</PerformanceTo>
<OrderedShareClassReturns>
<OrderedShareClassReturn Name="Property L" Performance="11.074980" />
<OrderedShareClassReturn Name="UK Property Trust I Inc" Performance="10.512610" />
<OrderedShareClassReturn Name="UK Property Trust I Acc" Performance="10.466310" />
<OrderedShareClassReturn Name="UK Property Trust R Inc" Performance="9.725650" />
</OrderedShareClassReturns>
</TopTen>
Now I need to add a second <ShareClassReturns>
node to the XML, and I'd like to add an attribute to that element, so the XML becomes:
<?xml version="1.0" encoding="utf-8"?>
<TopTen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SectorName>Property - Direct UK</SectorName>
<PerformanceTo>2011-01-31T00:00:00</PerformanceTo>
<OrderedShareClassReturns order="top"> // how can I add the "top" & "bottom" attributes?
<OrderedShareClassReturn Name="Property L" Performance="11.074980" />
<OrderedShareClassReturn Name="UK Property Trust I Inc" Performance="10.512610" />
<OrderedShareClassReturn Name="UK Property Trust I Acc" Performance="10.466310" />
<OrderedShareClassReturn Name="UK Property Trust R Inc" Performance="9.725650" />
</OrderedShareClassReturnss>
<OrderedShareClassReturns order="bottom">
<OrderedShareClassReturn Name="Property L" Performance="0.074980" />
<OrderedShareClassReturn Name="UK Property Trust I Inc" Performance="1.512610" />
<OrderedShareClassReturn Name="UK Property Trust I Acc" Performance="2.466310" />
<OrderedShareClassReturn Name="UK Property Trust R Inc" Performance="3.725650" />
</OrderedShareClassReturns>
</TopTen>
But I don't know how to do this. The objects that I'm serializing are defined as:
public class TopTen
{
public string SectorName { get; set; }
public DateTime PerformanceTo { get; set; }
public List<OrderedShareClassReturn> OrderedShareClassReturns { get; set; }
}
public class OrderedShareClassReturn
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public decimal Performance { get; set; }
}
So the question is, how can I add that attribute to the <OrderedShareClassReturns>
element? Do I need to define a OrderedShareClassReturns
as a class that implements a List<OrderedShareClassReturn>
, and have a property called order
, which has [XmlAttribute]
? Or is there an easier way?
To do that you'd need to add another class:
and have: