• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

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

0
troyhalsey
Asked:
troyhalsey
  • 4
  • 3
1 Solution
 
CDCOPCommented:
0
 
troyhalseyAuthor Commented:
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?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
troyhalseyAuthor Commented:
so this method doesn't even need xpath?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
troyhalseyAuthor Commented:
Okay...so say I want to delete the parent node of the selected node......so delete the entire customer David entry?

0
 
Carl TawnSystems and Integration DeveloperCommented:
Following on from the previous sample:

    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 + "']")

        If MessageBox.Show("Delete " + ListBox1.SelectedItem + ", are you sure?", "Delete Customer", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            node.ParentNode.RemoveChild(node)
        End If

    End Sub


So, to recap, this grabs the Customer node that it the parent of the Lastname selected from the listbox, prompts the user to delete. If the user says Yes the node is removed by calling the RemoveChild method of its parent node, passing itself as the argument.

Of course, you would then need to call save on the XmlDocument to reflect the changes to the file.
0
 
troyhalseyAuthor Commented:
You are the man, carl.  Thank you so much.

Troy
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now