Solved

How to make a XPathNav select a specific node ?

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now