从具有相同名称的多个节点中的单个节点进行解析

时间:2016-02-17 09:26:00

标签: c# xml parsing

  <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>

Hello Dears ..我有一个xml文件。它包含具有相同节点名称的不同节点。在physicalResource节点下的示例中,我想提取prName和所有特征的名称和值。但我无法分开解析他们。 我正在使用

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

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

4 个答案:

答案 0 :(得分:1)

您可以使用xmldocument并将该xml加载到xmldocument,然后您可以使用electinglenode。这会有所帮助!!

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

答案 1 :(得分:0)

使用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();

        }
    }
}

答案 2 :(得分:0)

要选择具有第一个prName节点的相应characteristic节点的所有physicalResourceCharacteristic个节点,您将在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表达式的计数,以获得包含其中几个条目的节点集。或者您可以省略[1]以获取一个节点集,其中所有physicalResource的前缀为prName,但具有混合节点类型。

答案 3 :(得分:0)

谢谢大家。我已经使用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;
                }
            }
相关问题