从具有相同名称的多个节点中的单个节点进行分析
本文关键字:节点 单个 | 更新日期: 2023-09-27 18:34:53
<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);
它提取两个节点的所有特性值。如何从单个物理资源节点中提取这些参数?
您可以使用 xmldocument 并将该 xml 加载到 xmldocument,然后您可以使用 selectsinglenode。这会有所帮助!!
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
xdoc.DocumentElement.SelectSingleNode(path for the node..);
Use 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();
}
}
}
要选择具有第一个physicalResourceCharacteristic
节点的相应characteristic
节点的所有prName
节点,请在 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>
我不确定这是否是你努力的目标。您可以遍历构造[xxx]
XPath 表达式physicalResource
计数,以获得包含其中几个条目的节点集。或者,您可以省略[1]
,以获取所有physicalResource
都以 prName
s 为前缀但具有混合节点类型的节点集。
谢谢大家。我已经通过使用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;
}
}