SelectNodes twice

S

sam

I need to make a SelectNodes and in the result set make a second
SelectNodes:

XmlNodeList nodeList = doc.DocumentElement.SelectNodes(xPathQuery);
Now I need to make a second .SelectNodes(xPathQuery);

How can I do that because nodeList does not have a SelectNodes method ?

Thanks for help.
Sam
 
M

Marc Gravell

Can you give an example of the 2 queries? Most times, xpath can cope
with both at once... it is very flexible ;-p

If this isn't possible (for some context-specific reason) then perhaps
you might have to enumerate the first list, evaluating the 2nd xquery,
and dumping the successive results into a list.

Finally - remember that you can (in some scenarios) add helper
(extension) objects to the mix, so that you can include arbitrary C#
code in your expression - i.e. "freds:superMethod(.)" (where "freds"
is an alias to a namespace that represents your extension object).

Marc
 
S

sam

Marc Gravell said:
Can you give an example of the 2 queries? Most times, xpath can cope
with both at once... it is very flexible ;-p
I use in my csharp code this xml:
<doc>
<item Id="1">
<item>1</item>
</item>
<item Id="1">
<item>2</item>
</item>
<item Id="2">
<item>A</item>
</item>
<item Id="3">
<item>B</item>
</item>
</doc>

I use this /doc/item[@Id != "1" and (position() >0 and position() <= 2) ]
but there is no result, why ?
Sam
 
M

Marc Gravell

xquery is 1-based (not 0-based like C#); nodes 1 & 2 have @Id = "1",
so are excluded. nodes 3 & 4 (@Id = 2, 3) fail because of the
position().

I'll double check with an example... give me a sec...

Marc
 
M

Marc Gravell

This returns node A; is this what you are after?

doc.LoadXml(xml);
foreach (XmlNode node in doc.SelectNodes(@"/doc/item[@Id !
=""1"" and (position() > 1 and position() <= 3)]")) {
Console.WriteLine(node.OuterXml);
}
 
S

sam

Marc Gravell said:
xquery is 1-based (not 0-based like C#); nodes 1 & 2 have @Id = "1",
so are excluded. nodes 3 & 4 (@Id = 2, 3) fail because of the
position().
Okey but my needs is to get from the result set a range of nodes that start
at the first position, because I don't know by advance how many nodes have
been excluded.

Sam
 
M

Marc Gravell

Okey but my needs is to get from the result set a range of nodes that start
at the first position, because I don't know by advance how many nodes have
been excluded.

Oh, that's even easier; for the first only:
@"/doc/item[@Id ! =""1""][1]

For the first 2:
@"/doc/item[@Id ! =""1""][position() <= 2]

Fortunately, position() refers to the position *within the current
working set*, and not the fixed position in the document.

Gotta love it,

Marc
 

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