在 XML 文档中查找兄弟属性 [英] Find Sibling Attribute in XML document

查看:45
本文介绍了在 XML 文档中查找兄弟属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的程序 WonderWare 创建了以下 XML 文档

A program I'm using, WonderWare, creates the following XML document

<ItemsList>
    <Item Name="31" Alias="PMP1_ANY_FAULT"/>
    <Item Name="29" Alias="PMP1_DEVIATION"/>
    <Item Name="27" Alias="PMP1_DISCREPANCY"/>
    <Item Name="25" Alias="PMP1_EQUIP_SC_BAD"/>
    <Item Name="402019 F" Alias="PMP1_EQUIP_SP"/>
</ItemsList>

使用 VB.Net 我可以打开文档,但我无法弄清楚如何通过它的别名查找索引,然后返回关联的项目名称.老实说,我什至找不到像这个程序那样将两个元素推到一起的例子,所以我觉得我只是在尝试查找信息时在黑暗中随机刺伤.

Using VB.Net I can open the document but I cannot figure out how to look up an index by it's alias and then return the associate Item name. Honestly, I can't even find an example where two elements are shoved together the way this program is, so I feel like I'm just stabbing randomly in the dark while trying to look up information.

目前我正在使用 xPath 与 XML 文件交互,但如果其他方法效果更好,我愿意尝试其他方法.

At the moment I'm using xPath to interact with the XML file, but if something else will work better, I'm willing to try something else.

更改了问题标题以更准确地反映我需要帮助的内容.

Changed question title to more accurately reflect what I needed help with.

推荐答案

感谢 @Crowcode 和 @Phrogz 为我指明了正确的方向.这是最终的代码

Thank-you @Crowcode and @Phrogz for pointing me in the right direction. Here's the final code

dim v as string;
dim ItemDBXMLString as string;
dim doc as System.Xml.XmlDocument;
dim SR as System.IO.StringReader;
dim TR as System.Xml.XmlTextReader;
dim nodeList as System.Xml.XmlNodeList;
dim root as System.Xml.XmlNode;
dim Node as System.Xml.XmlNode;
dim x as integer;

ItemDBXMLString = MB_TCPIP.PLC_001.AliasDatabase;
doc = new System.Xml.XmlDocument;
SR = new System.IO.StringReader(ItemDBXMLString);
TR = new System.Xml.XmlTextReader(SR);
doc.Load(TR);
root = doc.DocumentElement;

Address = root.SelectSingleNode("/ItemsList/Item[@Alias='PMP1_SI_VALUE']/@Name").InnerText;

我必须明确使用 System.XML,因为 WonderWare 不允许我使用导入.除此之外,我正在获取 XML文件"(实际上只是一个大字符串)并在其中搜索特定的别名.由于我所有的别名值都是唯一的,因此我只需要使用 select 单个节点.然后返回关联(兄弟姐妹?)姓名.

I had to explicitly use System.XML because WonderWare wouldn't let me make use of imports. Beyond that I'm taking the XML "file" (which is actually just a big string) and searching it for a specific alias name. Since all my Alias values are unique I only need to use select single node. Which then returns the associate (sibling?) Name.

再次感谢您的帮助!

这篇关于在 XML 文档中查找兄弟属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆