473,372 Members | 1,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

how to find nodes in xml file using xpath


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) & "')"
  8.             If j < d.GetUpperBound(0) Then
  9.                 xpathexpression = xpathexpression & " or "
  10.             End If
  11.         Next
  13. Dim xdoc As New XPathDocument(AppDomain.CurrentDomain.BaseDirectory + "/testxml.xml")
  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
  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
  2.   <CONTRACT>
  3.    <ID>779</ID>
  4.    <NAME>ContractName779</NAME>
  6.      <STARTDATE>1/8/2005</STARTDATE>
  7.      <ENDDATE>31/7/2008</ENDDATE>
  8.      <COMMODITIES>
  9.        <COMMODITY>
  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>
  21.      <CONTRACTKEYWORDS>Chemistry, Engineering, Chemical</CONTRACTKEYWORDS>
  22.       </CONTRACTTERMS>
  23.   </CONTRACT>
  24. </CONTRACTS>
Mar 1 '10 #1
5 3461
8,658 Expert Mod 8TB
sorry, canít help with that programming language.
Mar 1 '10 #2
2,057 Expert 2GB
You're filtering on child CONTRACTKEYWORDS. However in your xml you have:
Expand|Select|Wrap|Line Numbers
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:


Many thanks...
Mar 1 '10 #4
2,057 Expert 2GB
Context. What you are selecting:
-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:
Mar 1 '10 #5
Thanks for your help. It worked.
Mar 3 '10 #6

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

Similar topics

by: Johannes Lebek | last post by:
Hi there, somehow, I cannot access nodes that are stored in a variable. I'm using Xalan 2.5.1 and the following commands: ================ BEGIN ==================== <xsl:variable...
by: inquirydog | last post by:
Hi- Does anyone know a way to compare whether two nodes contain the same information in xslt (the name, attributes, and all content recursivly should be the same. I am interested in the case...
by: serge calderara | last post by:
Dear all, I have a csv file data which has been read and populate on a dataset object. then I need to bind part of the content of the dataset to a treeview control. I have read that XML format...
by: tMan | last post by:
whats the xpath to get all the empty nodes in a document. in the xml below i want to get the nodes <empty> and <t> all other nodes either have a child node or text in them <root> <test> <se>...
by: Paul J Lay | last post by:
I am using the XmlDocument Select Nodes method and it seems to work OK except that it returns embedded nodes in the collection when I only want the nodes that are not embedded. For example I have...
by: Guy | last post by:
Hi, I have an XML file which its content can't be altered as a source. My solution should be able to produce some kind of pointers (in a seperate file) to specific nodes (or even...
by: Curious | last post by:
Hi, I have the following XML file. Now I am using the code below to return the number of disticnt ProcessID's in the XML file. The result being returned is the...
by: Vikram Vamshi | last post by:
If I execute XmlNode.SelectNodes("some xpath query") and then use foreach to iterate over it. Will it guaratee that the order of nodes I get will be same as the order of nodes in the original xml...
by: TT (Tom Tempelaere) | last post by:
Hi all, I created an XSD to define the structure of an XML file for my project. I made an XML file linked to the XSD using XmlSpy. The problem is that if I read the file using .NET XmlDocument...
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, can someone please let me know how I can read xml elements using object oriented program. I created a class to use the get and set properties however I dont know how I can pass the values from...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.