C#获取xml值的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#获取xml值的问题相关的知识,希望对你有一定的参考价值。

<root>
<Activity id="55">
<style>·</style>
<topic title="sddsfdffd" type="单选">
<subject state="0" count="0">sdfdsfdsfd</subject>
<subject state="0" count="0">ssddfssdffsd</subject>
<subject state="0" count="0">sdfsdfsdfs</subject>
</topic>
</Activity>
</root> 获取subject 节点里的值,循环显示出来。

string xml = "<root><Activity id=\\"55\\"><style></style><topic title=\\"sddsfdffd\\" type=\\"单选\\"><subject state=\\"0\\" count=\\"0\\">sdfdsfdsfd</subject><subject state=\\"0\\" count=\\"0\\">ssddfssdffsd</subject><subject state=\\"0\\" count=\\"0\\">sdfsdfsdfs</subject></topic></Activity></root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNodeList list = doc.DocumentElement.SelectNodes("//subject");
if (list != null)

foreach (XmlNode node in list)

Response.Write("<p>" + node.InnerText + "</p>");



我想你的问题主要是不知道怎么查找XML,XML查找语法为XPath,在这里有全面的说明:
http://msdn.microsoft.com/zh-cn/library/ms256471(VS.80).aspx
参考技术A 使用XMLDocument.GetelementByID("subject ")获取到所有subject 节点的集合然后Foreach这个集合然后使用item.InnerText就能读取该节点的内容。

如何在将 xml 反序列化为 c# 对象时获取单个 xml 元素的多个值?

【中文标题】如何在将 xml 反序列化为 c# 对象时获取单个 xml 元素的多个值?【英文标题】:How do I get multiple value of single xml element while deserializing xml to c# object? 【发布时间】: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" 属性。

谁能帮我获得价值!

【问题讨论】:

【参考方案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; 

【讨论】:

谢谢。有效。我现在可以拿到价格了。

以上是关于C#获取xml值的问题的主要内容,如果未能解决你的问题,请参考以下文章

C# - 具有属性和节点值的 Xml 元素

使用 xmlReader 在 C# 中过滤特定元素值的大型 XML

XSD2Code++,从 XML 枚举到没有顺序值的 C# 枚举

在C#中获取对字段值的引用[重复]

在c#中获取所有枚举值的简单方法

在 php 和 xml 中获取包含特定值的 xml 节点数