Solved

How to make a XPathNav select a specific node ?

Posted on 2004-04-28
7
359 Views
Last Modified: 2012-05-04
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
Comment
Question by:Jarodtweiss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 4

Author Comment

by:Jarodtweiss
ID: 10939906
I tried
  descendant::ITEMS[ITEM/TXT_CODE='txt_code2']
  descendant::ITEM[TXT_CODE='txt_code2']
but I always have the first node
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 10941612
m_XmlIte = m_XmlDoc.CreateNavigator.Select("/ITEMS/ITEM/TXT_CODE")
0
 
LVL 4

Author Comment

by:Jarodtweiss
ID: 10941936
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
 
LVL 4

Author Comment

by:Jarodtweiss
ID: 10941979
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10973053
Closed, 250 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question