• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

How to make a XPathNav select a specific node ?

Hello all !

I have the following XML file :

<?xml version="1.0" encoding="utf-8" ?>
<ITEMS count="5">
      <ITEM>
            <TXT_CODE>txt_code1</TXT_CODE>
            <VALUE><![CDATA[VALUE 1]]></VALUE>
      </ITEM>
      <ITEM>
            <TXT_CODE>txt_code2</TXT_CODE>
            <VALUE><![CDATA[VALUE 2]]></VALUE>
      </ITEM>
                ....
</ITEMS>

I do NOT want to use a dataset to load it and I do NOT want to use a XmlDocument.
I want to parse it that file using the XPathNavigator.

Previously, I was using
  Private m_XmlIte As XPathNodeIterator
        m_XmlDoc = New XPathDocument(New IO.StringReader(XmlLabels))
        m_XmlIte = m_XmlDoc.CreateNavigator.Select("descendant::ITEM")
and then iterating on the XPathNodeIterator to get my different values.

But I wanted to add a specific method to get directly the node I wanted (for instance, egtting firectly the second ITEM element without any MoveNext call)
And so I want to use something like :

    m_XmlIte = m_XmlDoc.CreateNavigator.Select("descendant::ITEMS[ITEM/TXT_CODE='" & txtCode & "']")

But this syntax always give me the first ITEM element.
What is the problem ? Can I do what I want with the XPathNavigator ? Is there a mistake in my XPathExpression ?

Thanks !
0
Jarodtweiss
Asked:
Jarodtweiss
  • 3
1 Solution
 
JarodtweissAuthor Commented:
I tried
  descendant::ITEMS[ITEM/TXT_CODE='txt_code2']
  descendant::ITEM[TXT_CODE='txt_code2']
but I always have the first node
0
 
DranizzCommented:
m_XmlIte = m_XmlDoc.CreateNavigator.Select("/ITEMS/ITEM/TXT_CODE")
0
 
JarodtweissAuthor Commented:
Dranizz,

your expression is correct if I want to iterate thru all TXT_CODE tags which I do not want to do !
I really want to arrive directly at the TXT_CODE I want.
I tried
  m_XmlDoc.CreateNavigator.Select("/ITEMS/ITEM/TXT_CODE='myCode'")
but that gives me an exception !
0
 
JarodtweissAuthor Commented:
I found the answer !
I use now :

    Dim myExpr As Xml.XPath.XPathExpression
    myExpr = myNav.Compile("descendant::ITEM[TXT_CODE='txt0008']")
    myIte = myNav.Select(myExpr)

I close the question
0
 
moduloCommented:
Closed, 250 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now