Xml: Finding the right node to modify

G

Guest

Hey there,

I'm having trouble finding the right XPath expression. The XML file I'm
parsing contains an element Heads which contains sereveral Head elements.
These Head elements have several Row sub-elements.

Heads ->(0..*)Head -> ...subelements... -> (0..*)Row

The problem I have is that I find the wrong Row elements, ie I always find
the FIRST Row element inside the complete Xml document. This is probably
caused by a bad XPath expression I gave :) Can you help me?

What I do is the following. Each Head has an index element (Index), each Row
also has an Index element. First I try to find the Head. I start searching
from the Heads node within the Xml file:

XmlNode headsNode = xmlDoc.SelectSingleNode( "//Heads" );
XmlNode baseHeadNode = headsNode.SelectSingleNode( "//Head[Index=i]" );
XmlNode rowNode = baseHeadNode.SelectSingleNode( "//Row[Index=0]" );

The i is a number between 0 and #heads-1. The last statement always gives me
the first Row with Index 0 inside the whole XML file, NOT the Row with Index
0 that is found under Head with Index i.

What is wrong with my XPath expression?
 
G

Guest

Hi again,

I found a solution, but I'm still wondering why the original code I posted
does not work.

I find the correct Row elements if I use the following code instead:

XmlNode headsNode = xmlDoc.SelectSingleNode( "//Heads" );
XmlNode rowNode = headsNode.SelectSingleNode (
"//Head[Index=i]//Row[Index=0]"
);

I fail to see what is so different between this way of looking up the Row
elements, and the way I posted earlier...

Thanks,
Tom T.
 
G

Guest

Hey,

Could it be that I had to write the lookup of the Row elements as follows?

XmlNode headsNode = xmlDoc.SelectSingleNode( "//Heads" );
XmlNode baseHeadNode = headsNode.SelectSingleNode( "//Head[Index=i]" );
XmlNode rowNode = baseHeadNode.SelectSingleNode( ".//Row[Index=0]" );

So let lookup of Row elements start explicitely from the Head element
context? I guess so...

Thanks,
Tom T.

TT (Tom Tempelaere) said:
Hi again,

I found a solution, but I'm still wondering why the original code I posted
does not work.

I find the correct Row elements if I use the following code instead:

XmlNode headsNode = xmlDoc.SelectSingleNode( "//Heads" );
XmlNode rowNode = headsNode.SelectSingleNode (
"//Head[Index=i]//Row[Index=0]"
);

I fail to see what is so different between this way of looking up the Row
elements, and the way I posted earlier...

Thanks,
Tom T.

TT (Tom Tempelaere) said:
Hey there,

I'm having trouble finding the right XPath expression. The XML file I'm
parsing contains an element Heads which contains sereveral Head elements.
These Head elements have several Row sub-elements.

Heads ->(0..*)Head -> ...subelements... -> (0..*)Row

The problem I have is that I find the wrong Row elements, ie I always find
the FIRST Row element inside the complete Xml document. This is probably
caused by a bad XPath expression I gave :) Can you help me?

What I do is the following. Each Head has an index element (Index), each Row
also has an Index element. First I try to find the Head. I start searching
from the Heads node within the Xml file:

XmlNode headsNode = xmlDoc.SelectSingleNode( "//Heads" );
XmlNode baseHeadNode = headsNode.SelectSingleNode( "//Head[Index=i]" );
XmlNode rowNode = baseHeadNode.SelectSingleNode( "//Row[Index=0]" );

The i is a number between 0 and #heads-1. The last statement always gives me
the first Row with Index 0 inside the whole XML file, NOT the Row with Index
0 that is found under Head with Index i.

What is wrong with my XPath expression?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top