Solved

How to make a XPathNav select a specific node ?

Posted on 2004-04-28
7
350 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input parameteres to DragOver 2 38
Visual Studio npm 1 29
PowerShell:  Column widths won't expand 3 50
Selenium: Transitioning from unit testing to automated testing 4 62
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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