Find Sibling Attribute in XML document

不羁的心 提交于 2019-12-12 06:12:38

问题


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>

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.

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.

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


回答1:


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;

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.

Thanks again for the assistance!



来源:https://stackoverflow.com/questions/28077102/find-sibling-attribute-in-xml-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!