Find an xml child node

peoman

Member
Joined
Oct 17, 2008
Messages
5
Programming Experience
Beginner
VB.NET:
<?xml version="1.0" encoding="UTF-8"?>
<games>
  <game id="001">
    <name>Warcraft III</name>
    <cdkey1>XXXX-XXXX-XXXX</cdkey1>
    <cdkey2>YYYY-YYYY-YYYY</cdkey2>
  </game>
  <game id="002">
    <name>Diablo II</name>
    <cdkey1>XXXX-XXXX-XXXX</cdkey1>
    <cdkey2>YYYY-YYYY-YYYY</cdkey2>
  </game>
</games>

If I know the <name> of the game, how can i find his <id> ?
 
Get the parent, get the id attribute.
 
Ok, I misunderstood and thought you knew the xml node <name> of the game. I don't think you know this node, so just do a XPath to select the id attribute node of game node where name node value is the string value you had in mind.
VB.NET:
Dim doc As New Xml.XmlDocument
doc.Load("data.xml")
Dim name As String = "Diablo II"
Dim xpath As String = String.Format("//game[name='{0}']/@id", name)
Dim id As String = doc.SelectSingleNode(xpath).Value
 
Back
Top