how to find nodes in xml file using xpath

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milecimm
    New Member
    • Oct 2007
    • 24

    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."

    Code:
    Dim d() As String
            Dim j As Integer
            d = TextBox1.Text.Split(" ")
            Dim xpathexpression As String = ""
            For j = 0 To d.GetUpperBound(0)
             xpathexpression = xpathexpression & "contains(CONTRACTKEYWORDS,'" & d(j) & "')"
             
                If j < d.GetUpperBound(0) Then
                    xpathexpression = xpathexpression & " or "
                End If
            Next
    
    Dim xdoc As New XPathDocument(AppDomain.CurrentDomain.BaseDirectory + "/testxml.xml")
    
            Dim nav As XPathNavigator = xdoc.CreateNavigator()
     Dim nodes As XPathNodeIterator = nav.Select("/CONTRACTS/CONTRACT/*[" & xpathexpression & "]")
     tr += "<tr><td>" & nav.SelectSingleNode("ID").Value & "</td>"
    For Each node As XPathNavigator In nodes
     tr += "<td>" & node.SelectSingleNode("CONTRACTKEYWORDS").Value & "</td></tr>"
    Next
    
    Dim th As String = "<th>Commodity</th><th>Name</th><th>Supplier</th><th>Name</th>"
            div1.InnerHtml = ("<table class='datatable1'>" & th) + tr & "</table>"
    My xml is:

    Code:
    <CONTRACTS>
      <CONTRACT>
       <ID>779</ID>
       <NAME>ContractName779</NAME>
    
         <STARTDATE>1/8/2005</STARTDATE>
         <ENDDATE>31/7/2008</ENDDATE>
         <COMMODITIES>
           <COMMODITY>
             <COMMODITYCODE>CHEM</COMMODITYCODE>
             <COMMODITYNAME>Chemicals</COMMODITYNAME>
           </COMMODITY>
         </COMMODITIES>
        <SUPPLIERS>
          <SUPPLIER>
            <SUPPLIERID>1298</SUPPLIERID>
            <SUPPLIERNAME>Supplier name</SUPPLIERNAME>
            </SUPPLIER>
          </SUPPLIERS>
        <CONTRACTTERMS>
         <CONTRACTKEYWORDS>Chemistry, Engineering, Chemical</CONTRACTKEYWORDS>
          </CONTRACTTERMS>
      </CONTRACT>
    </CONTRACTS>
    Last edited by Dormilich; Mar 1 '10, 01:19 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    sorry, can’t help with that programming language.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      You're filtering on child CONTRACTKEYWORD S. However in your xml you have:
      Code:
       <CONTRACTTERMS> 
           <CONTRACTKEYWORDS>
      You need to be filtering on CONTRACTTERMS/CONTRACTKEYWORD S,
      And selecting it too when outputting values.
      Code:
      xpathexpression = xpathexpression & "contains(CONTRACTTERMS/CONTRACTKEYWORDS,'" & d(j) & "')" 
      ...
      tr += "<td>" & node.SelectSingleNode("CONTRACTTERMS/CONTRACTKEYWORDS").Value & "</td></tr>"

      Comment

      • milecimm
        New Member
        • Oct 2007
        • 24

        #4
        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...

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          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

          Comment

          • milecimm
            New Member
            • Oct 2007
            • 24

            #6
            Hi
            Thanks for your help. It worked.

            Comment

            Working...