当 XML 节点从列表序列化时,如何向该节点添加属性?

发布于 2024-10-19 07:39:39 字数 3116 浏览 4 评论 0原文

我问了这个问题昨天,想知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2024-10-26 07:39:39

为此,您需要添加另一个类:

public class ReturnsWrapper {

    [XmlElement("OrderedShareClassReturn")]
    public List<OrderedShareClassReturn> Items { get; set; }

    [XmlAttribute("order")]
    public string Order {get;set;}
}

并具有:

public class TopTen
{

    public string SectorName { get; set; }
    public DateTime PerformanceTo { get; set; }
    [XmlElement("OrderedShareClassReturns")]
    public List<ReturnsWrapper> Returns { get; set; }
}

To do that you'd need to add another class:

public class ReturnsWrapper {

    [XmlElement("OrderedShareClassReturn")]
    public List<OrderedShareClassReturn> Items { get; set; }

    [XmlAttribute("order")]
    public string Order {get;set;}
}

and have:

public class TopTen
{

    public string SectorName { get; set; }
    public DateTime PerformanceTo { get; set; }
    [XmlElement("OrderedShareClassReturns")]
    public List<ReturnsWrapper> Returns { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文