Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Multiple Named Elements in XPath 1

Status
Not open for further replies.

Genimuse

Programmer
May 15, 2003
1,797
US
I'm parsing through an XML document and need to get multiple values from each item. A single item in the XML looks like this:
Code:
<Customer>
  <ID>1043</ID>
  <Name>Bob Smith</Name>
  <Provider>
    <ProviderID>860</ProviderID>
  </Provider>
</Customer>
I'm looking for records that match a particular ProviderID (in this example, = 860), so my bit of Visual Basic code looks like this (skip to the "xmlSourceList" line if you don't know VB):
Code:
Dim xmlDoc As New System.Xml.XmlDocument()
'Load the file
xmlDoc.Load(AppPath() & "\Customers.xml")
Dim xmlSourceList As System.Xml.XmlNodeList
Dim xmlSourceItem As System.Xml.XmlNode
'List of items that match
xmlSourceList = xmlDoc.SelectNodes("//ProviderID[. = '860']/../../Name")
'Iterate through the list
For Each xmlSourceItem In xmlSourceList
  txtDisplay.Text += vbCrLf & xmlSourceItem.InnerText
Next
This works fine for retrieving the name, but I want to retrieve the Name and the ID. My xpath knowledge is pretty basic, so I don't know if there's a better way to specify it to get both. I can't use "../../*" and get everything due to... well, it's complicated, but I need to be able to specify precisely which named values to return.

Is there a way besides "*" that I can use to specify multiple elements?
 
>I can't use "../../*" and get everything due to... well, it's complicated

It's all relative. Suppose the structure shown is sort generic enough, you can do this in classic vbs/vb.
[tt]
s = s & vbCrLf & xmlSourceItem.text & vbTab & xmlSourceItem.SelectSingleNode("./preceding-sibling::ID").text
[/tt]
(You just have to translate it to the same in .net)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top