根据节点ID从xml文件检索数据 [英] retrive data from xml file based on node id
本文介绍了根据节点ID从xml文件检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?xml version="1.0" encoding="utf-8"?>
<DataDownload>
<ClusterId id="00 15">
<attrib>00 00</attrib>
<attrib>01 00</attrib>
<attrib>02 00</attrib>
<attrib>03 00</attrib>
</ClusterId>
<ClusterId id="00 00">
<attrib>00 00</attrib>
<attrib>04 00</attrib>
<attrib>05 00</attrib>
<attrib>07 00</attrib>
</ClusterId>
<ClusterId id="07 02">
<attrib>00 00</attrib>
<attrib>00 02</attrib>
<attrib>00 03</attrib>
<attrib>03 03</attrib>
<attrib>06 03</attrib>
</ClusterId>
</DataDownload >
这是我的xml文件
我想根据集群ID节点ID来重新分配属性值.
例如,如果00 15 id表示
This is my xml file
i want to retieve attrib values based on cluster ID node id.
example if 00 15 id means
its childnodes values i need.
推荐答案
您可以使用XPath查询根据ID定位元素.例如:
You can use a XPath query to locate the element based on the id. For example something like:
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(@"
<DataDownload>
<ClusterId id=""00 15"">
<attrib>00 00</attrib>
<attrib>01 00</attrib>
<attrib>02 00</attrib>
<attrib>03 00</attrib>
</ClusterId>
<ClusterId id=""00 00"">
<attrib>00 00</attrib>
<attrib>04 00</attrib>
<attrib>05 00</attrib>
<attrib>07 00</attrib>
</ClusterId>
<ClusterId id=""07 02"">
<attrib>00 00</attrib>
<attrib>00 02</attrib>
<attrib>00 03</attrib>
<attrib>03 03</attrib>
<attrib>06 03</attrib>
</ClusterId>
</DataDownload >");
System.Xml.XmlNodeList result = xmlDoc.SelectNodes("//ClusterId[@id='00 15']");
if (result.Count > 0) {
// Here you go through the child nodes of the first result (result[0])
}
这篇关于根据节点ID从xml文件检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文