Link to home
Start Free TrialLog in
Avatar of troyhalsey
troyhalsey

asked on

How to use xpath

Alright...i am newbie and learn via examples.

Everyone is telling me to use xpath, and I have bought books and read online and can not find a clear example of how to setup xpath in my code.  

How do I write code for xpath?  I have seen all of the <<blah::blah>> examples...but where does that go?  Can anyone provide with a clear example of a sub using xpath?  Thanks.

Troy

Avatar of CDCOP
CDCOP

Avatar of troyhalsey

ASKER

Please stop referring me to another website, I have been there repeatedly.  Can someone show me a practical example of how to navigate from node to node.

Say I have a text box with text that = the content of a node in a xml file.  And I want to go to that node.  What would that code look like?
Avatar of Carl Tawn
Say you have the following XML document saved in a file called "C:\Customers.xml":

<Customers>
  <Customer ID="1">
    <Firstname>Bob</Firstname>
    <Lastname>Smith</Lastname>
  </Customer>
  <Customer ID="2">
    <Firstname>Ben</Firstname>
    <Lastname>Jones</Lastname>
  </Customer>
  <Customer ID="3">
    <Firstname>David</Firstname>
    <Lastname>Brent</Lastname>
  </Customer>
  <Customer ID="4">
    <Firstname>John</Firstname>
    <Lastname>Doe</Lastname>
  </Customer>
  <Customer ID="5">
    <Firstname>Jane</Firstname>
    <Lastname>Doe</Lastname>
  </Customer>
</Customers>

You could then add each customer to a list box with something like:

        Dim doc As New XmlDocument()
        doc.Load("C:\Customers.xml")

        Dim nodes As XmlNodeList = doc.SelectNodes("//Customer")
        Dim sName As String

        For Each customer As XmlNode In nodes
            sName = customer.ChildNodes(1).InnerText)
            ListBox1.Items.Add(sName)
        Next

Then, using the listboxes SelectedIndexChanged event, you could grab the Customer node corresponding to the Name selected in the list:

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim node As XmlNode
        node = doc.SelectSingleNode("//Customer[Lastname='" + ListBox1.SelectedItem + "']")

    End Sub
so this method doesn't even need xpath?
It uses XPath:

    "//Customers"

And:

    "//Customer[Lastname='Smith']"       (as it might equate)

Are both XPath expressions used, in the first example, to retrieve all Customer nodes, and, in the second sample, a single Customer node based on the value of the child "Lastname" node.
Okay...so say I want to delete the parent node of the selected node......so delete the entire customer David entry?

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are the man, carl.  Thank you so much.

Troy