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
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
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?
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?
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</Firstnam e>
<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("//Custome r")
Dim sName As String
For Each customer As XmlNode In nodes
sName = customer.ChildNodes(1).Inn erText)
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_SelectedIndexChan ged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChan ged
Dim node As XmlNode
node = doc.SelectSingleNode("//Cu stomer[Las tname='" + ListBox1.SelectedItem + "']")
End Sub
<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</Firstnam
<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("//Custome
Dim sName As String
For Each customer As XmlNode In nodes
sName = customer.ChildNodes(1).Inn
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_SelectedIndexChan
Dim node As XmlNode
node = doc.SelectSingleNode("//Cu
End Sub
ASKER
so this method doesn't even need xpath?
It uses XPath:
"//Customers"
And:
"//Customer[Lastname='Smit h']" (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.
"//Customers"
And:
"//Customer[Lastname='Smit
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.
ASKER
Okay...so say I want to delete the parent node of the selected node......so delete the entire customer David entry?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are the man, carl. Thank you so much.
Troy
Troy
http://builder.com.com/5100-22-5035074.html