473,503 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Issue selecting the correct nodes

Hi, I should probably start with my XML as it makes it easier to
explain the problem.
<?xml version="1.0" encoding="utf-8" ?>
<aa note="top level">

<b1 note="b1">
<c1 note="b1 c1">
<d1 note="b1 c1 d1" />
</c1>
<c2 note="b1 c2">
<d1 note="b1 c2 d1" />
</c2>
</b1>

<b2>
<c1 note="b2 c1">
<d1 note="b2 c1 d1" />
</c1>
<c2 note="b2 c2">
<d1 note="b2 c2 d1" />
</c2>
</b2>

<b3>
<c1 note="b3 c1">
<d1 note="b3 c1 d1" />
</c1>
</b3>

</aa>
Essentially I wish to select one b node, pass that node to another
method and then carry out operations on its sub nodes.

Here's some test code, the problem is in the last line where it tries
to assign list3.

XmlDocument doc = new XmlDocument();
doc.Load("c:\\a.xml");
XmlNode node = doc.SelectSingleNode(@"/aa/b1");
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(node.OuterXml);
XmlNodeList list = doc2.SelectNodes(@"/b1/c1/*"); //works, 1 node
XmlNodeList list2 = node.SelectNodes(@"/aa/b1/c1/*"); //works, 1 node
XmlNodeList list3 = node.SelectNodes(@"/b1/c1/*"); //want to work, 0
nodes :(

My problem is, the method passed the b node will not know from where
it came, and I would prefer to keep it that way. It just seems a bit
counter intuitive to me to select on a node and it not to be the root.

Anyway, any help would be appreciated.

Thanks
Oct 24 '08 #1
1 1705
DeveloperX wrote:
XmlDocument doc = new XmlDocument();
doc.Load("c:\\a.xml");
XmlNode node = doc.SelectSingleNode(@"/aa/b1");
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(node.OuterXml);
XmlNodeList list = doc2.SelectNodes(@"/b1/c1/*"); //works, 1 node
XmlNodeList list2 = node.SelectNodes(@"/aa/b1/c1/*"); //works, 1 node
XmlNodeList list3 = node.SelectNodes(@"/b1/c1/*"); //want to work, 0
nodes :(

My problem is, the method passed the b node will not know from where
it came, and I would prefer to keep it that way. It just seems a bit
counter intuitive to me to select on a node and it not to be the root.
Well if SelectNodes or SelectSingleNode would consider the node they are
called on the root node then you could never access any parent or
ancestor nodes.
So if you want to use XPath and SelectNodes/SelectSingleNode then you
have to live with '/' selecting the root node (the document node in the
DOM model). So you need to use
XmlNodeList list3 = node.SelectNodes(@"c1/*");

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 24 '08 #2

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

Similar topics

9
16599
by: Tjerk Wolterink | last post by:
Hello, suppose i have a dom like this: <a> - <b> - <b> - <d> - </b> - <c>
2
1552
by: Dag | last post by:
Hi I am pretty much an xml beginner; hopefully someone can easily answer this one... I want to select a node representing a worksheet in an xml document for the Office Web Components spreadsheet....
2
1464
by: Saurabh Sharma | last post by:
Hi, I am using Dom Parsing in Xml. I am the parent node and it has many children and each children has many children. I want to select children with a given name . Is there any method by which we...
6
1958
by: Thomas Tomiczek | last post by:
Ok, working my way through a complex library conversion to .NET 2.0 and C# 2.0 with generics I am totally stuck on one thing -if anyone sees the issue I would be more than glad. The situation is...
1
1735
by: Stefan | last post by:
Hey guys ! Can someone pls help me and tell me how it is possible to Select a Node in 2nd row? I got a treeview which is build like follows: node2.Nodes.Add("blah", "blubb");...
7
1307
by: PatrickRThomas | last post by:
I need help selecting nodes while excluding some of them. Here's an example of XML: <my_xml> <all_items> <item> <key>1</key> <name>Item 1</name> </item> <item>
1
1805
by: renfrochris | last post by:
I'm having difficulty finding the correct syntax that will allow me to select a group of invoices based on the total of an amount column located in its line items. Below are simplified examples of...
4
1840
by: Alexander Adam | last post by:
Hello folks, I got a few question on some basic data structure stuff. Sorry if those questions might sound too easy or such but after googling a lot I didn't find a real answer to all those...
11
9835
by: Michael Bray | last post by:
I'm playing around with XElement stuff, and I've come across a difficulty. The XML document that I'm reading contains an xmlns declaration on the main node... <root xmlns="http://www.me.com">...
0
7201
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
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
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
5578
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
5011
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
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
379
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.