【问题标题】:Getting XML Attributes Value in C#在 C# 中获取 XML 属性值
【发布时间】:2020-04-16 00:12:12
【问题描述】:

我有这个 xml

<?xml version="1.0" encoding="UTF-8"?>
<rsixml2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dtd.riege.com/rsixml2/rsixml2 http://dtd.riege.com/rsixml2/rsixml2.xsd" xmlns="http://dtd.riege.com/rsixml2/rsixml2">
<!--    2.78  20170509 RSIXML2 -->
<!--    $Header: /export/cvs/procars/product/protrack/xml/rsixml2.dbs,v 1.217 2017/05/09 14:53:44 schaefer Exp $ -->
        <metadata>
                <processing>
                        <process name="AI2XML" version="1.90"/>
                        <process name="RSIXML2" version="2.78"/>
                </processing>
                <params>
                </params>
        </metadata>
<!--    generated by 1.90  20181218 AI2XML  /$Header: /export/cvs/procars/incs/misc/fillxml.dbs,v 1.58 2017/02/28 14:05:47 brink Exp $ -->
<!--    Debug info. Branch BIRDXB: plb -d  AI2XML   -sno=1202000626 -msgtyp=AIHub2Branch -sid=BIRDXB -rid=GLOBLI -FINE -rsixml2 -->
        <partners>
                <partner qual="sender" value="BIRDXB"/>
                <partner qual="receiver" value="GLOBLI"/>
        </partners>
        <shipment messageType="AIHub2Branch" status="new" type="house">
                <partners>
                        <partner qual="sender" value="BIRDXB" role="importer"/>
                        <partner qual="receiver" value="GLOBLI"/>
                        <partner qual="finalDestination" value="GLOBLI"/>
                        <partner qual="exportGateway" value="BIRCGN"/>
                        <partner qual="shipmentCreator" value="BIRCGN"/>
                </partners>
                <refs>
                        <ref qual="shipper" type="shipper" value="20208098479"/>
                        <ref qual="shipper" type="shipper" value="8400649"/>
                        <ref qual="shipment" value="1202000626" system="true"/>
                        <ref qual="procarsId" value="BIRDXB1202000626" system="true"/>
                        <ref qual="ptt" value="BIRCGN20001198693" system="true"/>
                        <ref qual="awb" value="CGN-95629985" system="true"/>
                        <ref qual="mawb" value="501-06663753" system="true"/>
                </refs>
        </shipment>
</rsixml2>

我需要来自这里的值属性的值

<ref qual="mawb" value="501-06663753" system="true"/>

这是到目前为止的代码。

   XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Users\\shaki\\Desktop\\input.xml");

            foreach (XmlNode node in doc.DocumentElement)
            {
                if (node.Name == "ref")
                {
                    string name = node.Attributes[1].InnerText;
                checkedListBox2.Items.Add(name);
            }      

                    textBox1.Text += node;

            }

这些是 doc.DocumentElement 中的值

System.Xml.XmlCommentSystem.Xml.XmlCommentSystem.Xml.XmlElementSystem.Xml.XmlCommentSystem.Xml.XmlCommentSystem.Xml.XmlElementSystem.Xml.XmlElement

我不明白为什么。

我没有尝试过 LINQ,因为我不了解它,但如果它是最后的选择。我会接受的。

【问题讨论】:

标签: c# xml


【解决方案1】:

在 Youtube 的评论区找到这个。

XmlTextReader xtr = new XmlTextReader("C:\\Users\\shaki\\Desktop\\input.xml");


            string mawb = "";
            string mawb_value = "";
            string awb = "";
            string awb_value = "";
            while (xtr.Read())
            {

                if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "ref")
                {
                    xtr.MoveToAttribute("qual");
                    if(xtr.Value == "awb")
                    {
                        awb = xtr.Value;
                        xtr.MoveToAttribute("value");
                        awb_value = xtr.Value;
                        xtr.MoveToAttribute("qual");

                    }
                    if (xtr.Value == "mawb")
                    {
                        mawb = xtr.Value;
                        xtr.MoveToAttribute("value");
                        mawb_value = xtr.Value;
                        break;
                    }

                }
            }
            checkedListBox2.Items.Add(mawb + ":" + mawb_value+" "+awb+":"+awb_value);

【讨论】:

    【解决方案2】:

    我建议您使用 LINQ to XML
    Here 是 MSDN 的一个很好的例子。

    特别是对于您的具体示例,假设我们有以下 XML 的简化版本:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <partners>
        <partner qual="sender" value="BIRDXB"/>
        <partner qual="receiver" value="GLOBLI"/>
      </partners>
      <shipment messageType="AIHub2Branch" status="new" type="house">
        <partners>
          <partner qual="sender" value="BIRDXB" role="importer"/>
          <partner qual="receiver" value="GLOBLI"/>
          <partner qual="finalDestination" value="GLOBLI"/>
          <partner qual="exportGateway" value="BIRCGN"/>
          <partner qual="shipmentCreator" value="BIRCGN"/>
        </partners>
        <refs>
          <ref qual="shipper" type="shipper" value="20208098479"/>
          <ref qual="shipper" type="shipper" value="8400649"/>
          <ref qual="shipment" value="1202000626" system="true"/>
          <ref qual="procarsId" value="BIRDXB1202000626" system="true"/>
          <ref qual="ptt" value="BIRCGN20001198693" system="true"/>
          <ref qual="awb" value="CGN-95629985" system="true"/>
          <ref qual="mawb" value="501-06663753" system="true"/>
        </refs>
      </shipment>
    </root>
    

    我们可以编写以下 C# 代码来获取特定元素:

    var xDoc = XElement.Load("address.xml"); 
    
    var address = (
        from el in xDoc.Descendants("ref")
        where (string)el.Attribute("qual") == "mawb"
        select el
        ).FirstOrDefault();
    
    Console.WriteLine(address?.Attribute("value")?.Value);
    

    虽然值得一提的是,安静部分也可以通过扩展方法完成:

    xDoc.Descendants("ref")
        .FirstOrDefault(el => (string)el.Attribute("qual") == "mawb");
    

    【讨论】:

      【解决方案3】:

      使用 Xml Linq:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Xml;
      using System.Xml.Linq;
      
      namespace ConsoleApplication8
      {
          class Program
          {
              const string FILENAME = @"c:\temp\test.xml";
              static void Main(string[] args)
              {
                  XDocument doc = XDocument.Load(FILENAME);
                  XNamespace ns = doc.Root.GetDefaultNamespace();
      
                  List<Qual> partners = doc.Descendants(ns + "partner").Select(x => new Qual(x)).ToList();
                  List<Qual> refs = doc.Descendants(ns + "ref").Select(x => new Qual(x)).ToList();
      
              }
          }
      
          public class Qual
          {
              public string qual { get; set; }
              public string value { get; set; }
              public string role { get; set; }
              public string system { get; set; }
      
              public Qual(XElement element)
              {
                  foreach (XAttribute attribute in element.Attributes())
                  {
                      switch (attribute.Name.LocalName)
                      {
                          case "qual" :
                              qual = ((string)attribute);
                              break;
                          case "value":
                              value = ((string)attribute);
                              break;
                          case "role":
                              role = ((string)attribute);
                              break;
                          case "system":
                              system = ((string)attribute);
                              break;
                      }
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-16
        • 1970-01-01
        • 2021-11-17
        • 2016-11-05
        • 2011-07-26
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        相关资源
        最近更新 更多