473,608 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to find nodes in xml file using xpath

25 New Member
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 3469
Dormilich
8,658 Recognized Expert Moderator Expert
sorry, can’t help with that programming language.
Mar 1 '10 #2
jkmyoung
2,057 Recognized Expert Top Contributor
You're filtering on child CONTRACTKEYWORD S. However in your xml you have:
Expand|Select|Wrap|Line Numbers
  1.  <CONTRACTTERMS> 
  2.      <CONTRACTKEYWORDS>
  3.  
You need to be filtering on CONTRACTTERMS/CONTRACTKEYWORD S,
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
milecimm
25 New Member
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/CONTRACTKEYWORD S
ARE
..whatever...

Many thanks...
Mar 1 '10 #4
jkmyoung
2,057 Recognized Expert Top Contributor
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
milecimm
25 New Member
Hi
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

1
2745
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 name="referenced-node" select="//a"/> <xsl:variable name="attribute-source">
5
9541
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 where node ordering matters, and also the case where it doesn't, but perhaps that is an advanced topic). Ideally the method should be available to xpath expressions, so I think that creating new templates which compare nodes will not work (well,...
3
2170
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 is particular usefull to be bind to a treeview as it handle nodes. So I have try to save my dataset content into a xml file. The content of that file is as follow : - <NewDataSet> - <REC_INFO>
1
4281
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> <li>test</li> </se> </test> <bs>
6
1748
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 a statement as follows: nodes = doc.SelectNodes(\\Node1\\Node\\DesiredNode); It gives me back in the collection all the DesiredNode tags. If I select retrieve the nth node as follows node = nodes;
1
1206
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 attributes\values) in the source XML file. The source file contains many different namespaces (along with defaults). The output file later will need to be processed and rewrite sections of the XML source file by using the pointers ...
5
1271
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 'System.Xml.XPath.XPathSelectionIterator', while the result should be 2. Can someone help me out solve this problem.
8
12074
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 document? Can someone point me to a document which states that the order is (or is not) guarateened (I could not find any when i did my search on msdn). This is very crucial to our design, otherwise I will have to add a "sequence" attribute...
16
3513
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 and then query for the root element, the result is always null (1). However if I strip the root element of all attributes generated by XmlSpy, then there is no problem to find the root element with .NET XML classes (2). (1) The XML for which...
6
3211
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 xml file to the class and use the values in my form. Thanks -- Nejadian
0
8498
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8152
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5476
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1598
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1331
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.