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

troyhalseyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
troyhalseyAuthor Commented:
You are the man, carl.  Thank you so much.

Troy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.