简体   繁体   中英

Find Sibling Attribute in XML document

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM