473,497 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is context node?

Hi folks,

I'm diving into XPathNavigator.SelectDescendants method. There's a boolean
type parameter matchSelf, and MSDN says "To include the context node in the
selection, true; otherwise, false". But I don't understand how to use it.
The sample code in MSDN is below, but I didn't find any difference of the
result while setting the value of this parameter to true and false
inspectively. So could anybody show me how to use the parameter to get
different result?

Any reply is appreciated!
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book");
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText =
nodesNavigator.SelectDescendants(XPathNodeType.Tex t, false);

while (nodesText.MoveNext())
{
Console.Write(nodesText.Current.Name);
Console.WriteLine(nodesText.Current.Value);
}
<bookstore>
<book genre="autobiography" publicationdate="1981-03-22"
ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15"
ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
Dec 8 '05 #1
2 2391
nicholas wrote:
I'm diving into XPathNavigator.SelectDescendants method. There's a boolean
type parameter matchSelf, and MSDN says "To include the context node in the
selection, true; otherwise, false". But I don't understand how to use it.
The sample code in MSDN is below, but I didn't find any difference of the
result while setting the value of this parameter to true and false
inspectively. So could anybody show me how to use the parameter to get
different result?


The example is terrible, unfortunately. It doesn't actually move the
first iterator on, so it's actually selecting the descendants of the
top level node. Also, by using the text node type, it's not going to
show the difference between true and false, as the selected node will
never be a text node. Here's a modified version which shows the
difference:

using System;
using System.Xml;
using System.Xml.XPath;

class Test
{

static void Main()
{
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book");
nodes.MoveNext();
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator elements =
nodesNavigator.SelectDescendants(XPathNodeType.Ele ment,
false);

while (elements.MoveNext())
{
Console.WriteLine("Element name={0}",
elements.Current.Name);
}
}
}

This moves to the first node matching /bookstore/book, and then selects
all the element descendant nodes. As the second parameter is false, it
doesn't try to match the context node (the node you're searching for)
so the result doesn't include "Element name=book". If you change that
false to true, then it *does* include that.

Does that help?

Jon

Dec 8 '05 #2
Yes, that's really what I want to know. And I got the difference by
following your instruction.
Thanks very much for your kind-hearted help!

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
nicholas wrote:
I'm diving into XPathNavigator.SelectDescendants method. There's a
boolean
type parameter matchSelf, and MSDN says "To include the context node in
the
selection, true; otherwise, false". But I don't understand how to use it.
The sample code in MSDN is below, but I didn't find any difference of the
result while setting the value of this parameter to true and false
inspectively. So could anybody show me how to use the parameter to get
different result?


The example is terrible, unfortunately. It doesn't actually move the
first iterator on, so it's actually selecting the descendants of the
top level node. Also, by using the text node type, it's not going to
show the difference between true and false, as the selected node will
never be a text node. Here's a modified version which shows the
difference:

using System;
using System.Xml;
using System.Xml.XPath;

class Test
{

static void Main()
{
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book");
nodes.MoveNext();
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator elements =
nodesNavigator.SelectDescendants(XPathNodeType.Ele ment,
false);

while (elements.MoveNext())
{
Console.WriteLine("Element name={0}",
elements.Current.Name);
}
}
}

This moves to the first node matching /bookstore/book, and then selects
all the element descendant nodes. As the second parameter is false, it
doesn't try to match the context node (the node you're searching for)
so the result doesn't include "Element name=book". If you change that
false to true, then it *does* include that.

Does that help?

Jon

Dec 9 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2373
by: Vijay | last post by:
Hi Can anybody explain what self::* means in Xpath. I understand that "self" means the context node itself and * means any node type. So I thought self::* meant all the child nodes of context,...
4
11569
by: Phill | last post by:
Here's what I'm doing to make the right context mnue show up based on what node was cliked: private void tvwMenu_MouseUp(object sender, MouseEventArgs e) { //Select Node When Right Clicked &...
1
1423
by: Rory | last post by:
Hi - I've seen quite a few posts that almost match this but haven't found a solution that works. I have an explorer-like application with a treeview in the lefthand pane. When I right-click on a...
2
3710
by: Claire | last post by:
I've a treeview control containing a root node with several children on a windows form. I select a node with a left mouse button click. I then select one of the other nodes with right mouse click...
7
2788
by: Tim Hallwyl | last post by:
Hi, there! As I understand the XPaht recommendation, the context node is a node; not a node-list, not XPath object -- but a single node. Now, the WS-BPEL 2.0 specification allows an XML simple...
0
3272
by: divya1949 | last post by:
Create a windows c# application which will Read a xml file and populate nodes in the treeview. 1 On selection of treenode display the child nodes of that node in listview control 2. ...
0
1141
by: EvilProject | last post by:
Hi im writing a class of a context menu where each menu item is linked to a Tree Node. I have a click event (that belongs to the context menu class ,not the menu item class) that it's event...
5
1431
by: Darin | last post by:
My tree is a menu with the parents the different applications (AR, AP, GL, etc), under each application could be another sub-menu (code file, reports), or an actual selectable field (customer...
32
2689
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.