当 XML 节点从列表序列化时,如何向该节点添加属性?
我问了这个问题昨天,想知道如何使用 MVCContrib 生成带有属性的 XML。答案是使用[XmlAttribute]
。
此后,我已成功使用 [XmlAttribute]
获取以下 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>
现在我需要向 XML 添加第二个
节点,并且我将喜欢向该元素添加属性,因此 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 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>
但我不知道该怎么做。我要序列化的对象定义为:
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; }
}
所以问题是,如何将该属性添加到
元素?我是否需要将 OrderedShareClassReturns
定义为实现 List
的类,并具有名为 order
的属性,该属性具有 [XmlAttribute]
?或者有更简单的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您需要添加另一个类:
并具有:
To do that you'd need to add another class:
and have: