【问题标题】:Parse from single node in multiple nodes that have same name从具有相同名称的多个节点中的单个节点解析
【发布时间】:2016-02-17 09:25:59
【问题描述】:
  <physicalResource>
    <prName>PRS_EID</prName>
    <prDescription>PRS_EID</prDescription>
    <physicalResourceCharacteristic>
        <characteristic>
            <name>eidno</name>
            <value>SH001499000</value>
        </characteristic>
        <characteristic>
            <name>flatno</name>
            <value>14303</value>
        </characteristic>
    </physicalResourceCharacteristic>
  </physicalResource>

  <physicalResource>
    <prName>PRS_OLT</prName>
    <prDescription>PRS_OLT</prDescription>          
    <physicalResourceCharacteristic>
        <characteristic>
            <name>device</name>
            <value>WC-OMU-AO01</value>
        </characteristic>
        <characteristic>
            <name>frame</name>
            <value>1</value>
        </characteristic>
        <characteristic>
            <name>port</name>
            <value>5</value>                
        </characteristic>
    </physicalResourceCharacteristic>   
  </physicalResource>

你好,亲爱的.. 我有一个 xml 文件。它包含具有相同节点名称的不同节点。在物理资源节点下的示例中,我想提取 prName 和所有特征的名称和值。但我不能单独解析它们。 我正在使用

nodeListPrs = root.SelectNodes("/physicalResource/physicalResourceCharacteristic/characteristic", nsmgr); 

它提取两个节点的所有特征值。如何从单个物理资源节点中提取这些参数?

【问题讨论】:

    标签: c# xml parsing


    【解决方案1】:

    您可以使用 xmldocument 并将该 xml 加载到 xmldocument,然后您可以使用选择单节点。会有帮助的!!

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(xml);
    xdoc.DocumentElement.SelectSingleNode(path for the node..);
    

    【讨论】:

      【解决方案2】:

      使用 XML Linq

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Xml;
      using System.Xml.Linq;
      
      namespace ConsoleApplication78
      {
          class Program
          {
              const string FILENAME = @"c:\temp\test.xml";
              static void Main(string[] args)
              {
                  XDocument doc = XDocument.Load(FILENAME);
      
                  var results = doc.Descendants("physicalResource").Select(x => new {
                      prName = (string)x.Element("prName"),
                      characteristics = x.Descendants("characteristic").Select(y => new {
                             name = (string)y.Element("name"),
                             value = (string)y.Element("value")
                          }).ToList()
                      }).ToList();
      
              }
          }
      }
      

      【讨论】:

      • 感谢您的回答。但我可以提取 prName 的。我的问题是提取特征。我使用了选择节点(physicalResource/physicalResourceCharacteristic/characteristic)。但输出包含所有物理资源。我想让它们分别用于物理资源节点 1 和物理资源节点 2
      【解决方案3】:

      要选择所有prName 节点以及第一个physicalResourceCharacteristic 节点的对应characteristic 节点,您可以在SelectNodes 中使用以下XPath 表达式。

      /res/physicalResource[1]/physicalResourceCharacteristic/characteristic | /res/physicalResource[1]/prName
      

      结果是

      <?xml version="1.0" encoding="UTF-8"?>
      <prName>PRS_EID</prName>
      <characteristic>
          <name>eidno</name>
          <value>SH001499000</value>
      </characteristic>
      <characteristic>
          <name>flatno</name>
          <value>14303</value>
      </characteristic>
      

      我不确定这是否是您努力的目标。您可以遍历 physicalResource 构造 [xxx] XPath-expressions 的计数以获取包含这些条目的节点集。或者您可以省略 [1] 以获取所有 physicalResources 以 prNames 为前缀的节点集,但具有混合节点类型。

      【讨论】:

        【解决方案4】:

        谢谢大家。我已经通过使用 SelectSingleNode 和 SelectNodes 级联解决了:

        XmlNodeList nodeListPrs = root.SelectNodes("/ns2:serviceOrder/resourceFacingService/physicalResource", nsmgr);
                    foreach (XmlNode book in nodeListPrs)
                    {
                        string prsName = book["prName"].InnerText;
        
                        try
                        {
                            nodeListPrsCh = book.SelectSingleNode("physicalResourceCharacteristic").SelectNodes("characteristic");
        
                            foreach (XmlNode characteristics in nodeListPrsCh)
                            {
                                dataGridView3.Rows.Add();
                                dataGridView3.Rows[i].Cells[0].Value = prsName;
        
                                try { string name = characteristics["name"].InnerText; dataGridView3.Rows[i].Cells[1].Value = name; }
                                catch { dataGridView3.Rows[i].Cells[1].Value = "-"; }
        
                                try { string value = characteristics["value"].InnerText; dataGridView3.Rows[i].Cells[2].Value = value; }
                                catch { dataGridView3.Rows[i].Cells[2].Value = "-"; }
        
                                i = i + 1;
                            }
                        }
        
                        catch
                        {
        
                            dataGridView3.Rows.Add();
                            dataGridView3.Rows[i].Cells[0].Value = prsName;
                            dataGridView3.Rows[i].Cells[1].Value = "-";
                            dataGridView3.Rows[i].Cells[2].Value = "-";
                            i = i + 1;
                        }
                    }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          相关资源
          最近更新 更多