【问题标题】:How do I get multiple value of single xml element while deserializing xml to c# object?如何在将 xml 反序列化为 c# 对象时获取单个 xml 元素的多个值?
【发布时间】:2021-03-16 20:11:41
【问题描述】:

我正在从 xml 文件获取 xml 数据到 c# 对象,如下所示:

XML:

<OrderItem>
          <OrderItemCode>1234</OrderItemCode>
          <ASIN>dfsdfcs</ASIN>
          <SKU>5MJ1L3</SKU>
          <ItemStatus>Unshipped</ItemStatus>
          <ProductName>xcv/ProductName>
          <Quantity>1</Quantity>
          <ItemPrice>
             <Component>
                        <Type>Principal</Type>
                        <Amount currency="CAD">7.99</Amount>
             </Component>
          </ItemPrice>
</OrderItem>

c#模型:

[XmlRootAttribute("OrderItem")]
public class OrderItem
    {
        [XmlElement("OrderItemCode")]
        public string OrderItemCode { get; set; }

        [XmlElement("ASIN")]
        public string Asin { get; set; }

        [XmlElement("SKU")]
        public string Sku { get; set; }

        [XmlElement("ItemStatus")]
        public string ItemStatus { get; set; }

        [XmlElement("ProductName")]
        public string ProductName { get; set; }

        [XmlElement("Quantity")]
        public long Quantity { get; set; }

        [XmlElement("ItemPrice")]
        public ItemPrice Item_Price { get; set; }

        [XmlElement("PriceDesignation")]
        public string PriceDesignation { get; set; }

        [XmlElement("Promotion")]
        public Promotion Promotion { get; set; }

    }

    public partial class ItemPrice
    {
        [XmlElementAttribute("Component")]
        public List<Component> Component { get; set; }
    }

    public partial class Component
    {
        [XmlElement("Type")]
        public string Type { get; set; }

        [XmlElement("Amount")]
        public Amount Amount { get; set; }
    }

    public partial class Amount
    {
        [XmlAttribute("currency")]
        public string Currencies { get; set; }

        [XmlAttribute("#text")]
        public string Price { get; set; }
    }

反序列化:

 XmlSerializer serializer = new XmlSerializer(typeof(OrderItem));
 TextReader reader = new StreamReader(reportPath);
 OrderItem ordersListXML = (OrderItem)serializer.Deserialize(reader);

在这里,我想通过反序列化为 c# 对象来获取 &lt;Amount currency="CAD"&gt;7.99&lt;/Amount&gt; 的值,并且我能够将元素 &lt;Amount currency="CAD"&gt;7.99&lt;/Amount&gt; 的属性“货币”的值获取到“货币”属性但无法获取文本“ 7.99" 的 Element &lt;Amount currency="CAD"&gt;7.99&lt;/Amount&gt; 到反序列化后我的 c# 对象中的 "Price" 属性。

谁能帮我获得价值!

【问题讨论】:

    标签: c# xml xml-serialization xml-attribute xmlelement


    【解决方案1】:

    XmlTextAttribute ([XmlText]) 允许将条目的值反序列化为字段。所以

    <Amount currency="CAD">7.99</Amount>
    

    可以反序列化到类

    public class Amount
    {
        [XmlAttribute("currency")]
        public string Currencies { get; set; }
    
        [XmlText]
        public string Price { get; set; }
    }
    

    【讨论】:

    • 谢谢。有效。我现在可以拿到价格了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多