By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

Xml: Finding the right node to modify

P: n/a
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?
--
Tom Tempelaere.
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)" wrote:
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?
--
Tom Tempelaere.

Nov 16 '05 #2

P: n/a
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)" wrote:
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)" wrote:
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?
--
Tom Tempelaere.

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.