472,133 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

how to find nodes in xml file using xpath

Hi

I have a textbox that people would need to use to search a xml file. If a record is found with that worrd as keyword then the item is returned. I need to return ID, for example, but I dont seem to find the way to do this. Could you help? Thanks. This is what I have but I get "Object reference not set to an instance of an object."

Expand|Select|Wrap|Line Numbers
  1. Dim d() As String
  2.         Dim j As Integer
  3.         d = TextBox1.Text.Split(" ")
  4.         Dim xpathexpression As String = ""
  5.         For j = 0 To d.GetUpperBound(0)
  6.          xpathexpression = xpathexpression & "contains(CONTRACTKEYWORDS,'" & d(j) & "')"
  7.  
  8.             If j < d.GetUpperBound(0) Then
  9.                 xpathexpression = xpathexpression & " or "
  10.             End If
  11.         Next
  12.  
  13. Dim xdoc As New XPathDocument(AppDomain.CurrentDomain.BaseDirectory + "/testxml.xml")
  14.  
  15.         Dim nav As XPathNavigator = xdoc.CreateNavigator()
  16.  Dim nodes As XPathNodeIterator = nav.Select("/CONTRACTS/CONTRACT/*[" & xpathexpression & "]")
  17.  tr += "<tr><td>" & nav.SelectSingleNode("ID").Value & "</td>"
  18. For Each node As XPathNavigator In nodes
  19.  tr += "<td>" & node.SelectSingleNode("CONTRACTKEYWORDS").Value & "</td></tr>"
  20. Next
  21.  
  22. Dim th As String = "<th>Commodity</th><th>Name</th><th>Supplier</th><th>Name</th>"
  23.         div1.InnerHtml = ("<table class='datatable1'>" & th) + tr & "</table>"
My xml is:

Expand|Select|Wrap|Line Numbers
  1. <CONTRACTS>
  2.   <CONTRACT>
  3.    <ID>779</ID>
  4.    <NAME>ContractName779</NAME>
  5.  
  6.      <STARTDATE>1/8/2005</STARTDATE>
  7.      <ENDDATE>31/7/2008</ENDDATE>
  8.      <COMMODITIES>
  9.        <COMMODITY>
  10.          <COMMODITYCODE>CHEM</COMMODITYCODE>
  11.          <COMMODITYNAME>Chemicals</COMMODITYNAME>
  12.        </COMMODITY>
  13.      </COMMODITIES>
  14.     <SUPPLIERS>
  15.       <SUPPLIER>
  16.         <SUPPLIERID>1298</SUPPLIERID>
  17.         <SUPPLIERNAME>Supplier name</SUPPLIERNAME>
  18.         </SUPPLIER>
  19.       </SUPPLIERS>
  20.     <CONTRACTTERMS>
  21.      <CONTRACTKEYWORDS>Chemistry, Engineering, Chemical</CONTRACTKEYWORDS>
  22.       </CONTRACTTERMS>
  23.   </CONTRACT>
  24. </CONTRACTS>
Mar 1 '10 #1
5 3320
Dormilich
8,658 Expert Mod 8TB
sorry, can’t help with that programming language.
Mar 1 '10 #2
jkmyoung
2,057 Expert 2GB
You're filtering on child CONTRACTKEYWORDS. However in your xml you have:
Expand|Select|Wrap|Line Numbers
  1.  <CONTRACTTERMS> 
  2.      <CONTRACTKEYWORDS>
  3.  
You need to be filtering on CONTRACTTERMS/CONTRACTKEYWORDS,
And selecting it too when outputting values.
Expand|Select|Wrap|Line Numbers
  1. xpathexpression = xpathexpression & "contains(CONTRACTTERMS/CONTRACTKEYWORDS,'" & d(j) & "')" 
  2. ...
  3. tr += "<td>" & node.SelectSingleNode("CONTRACTTERMS/CONTRACTKEYWORDS").Value & "</td></tr>" 
Mar 1 '10 #3
Hi, thanks for your help.

My problem is how to get the Contract ID, name, etc as they are way back my path. How Can I get:
CONTRACTS/CONTRACT/CONTRACTID
CONTRACTS/CONTRACT/NAME
CONTRACTS/CONTRACT/COMMODITIES/COMMODITY/COMMODITYNAME
CONTRACTS/CONTRACT/SUPPLIERS/SUPPLIER/SUPPLIERNAME

WHERE
CONTRACTS/CONTRACT/CONTRACTTERMS/CONTRACTKEYWORDS
ARE
..whatever...

Many thanks...
Mar 1 '10 #4
jkmyoung
2,057 Expert 2GB
Context. What you are selecting:
nav.Select("/CONTRACTS/CONTRACT/*["
-Contract nodes.

For the rest of the nodes, you have to think about their position relative to the CONTRACT node. Pretty much just remove /CONTRACTS/CONTRACT/ from the front of the absolute xpath. eg:
ID
NAME
COMMODITIES/COMMODITY/COMMODITYNAME
SUPPLIERS/SUPPLIER/SUPPLIERNAME
Mar 1 '10 #5
Hi
Thanks for your help. It worked.
Mar 3 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Johannes Lebek | last post: by
5 posts views Thread by inquirydog | last post: by
3 posts views Thread by serge calderara | last post: by
6 posts views Thread by Paul J Lay | last post: by
1 post views Thread by Guy | last post: by
5 posts views Thread by Curious | last post: by
8 posts views Thread by Vikram Vamshi | last post: by
16 posts views Thread by TT (Tom Tempelaere) | last post: by
6 posts views Thread by =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.