473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to access the total number of child nodes from a parent node

MA
Hi,

How to access the total number of child nodes from a parent node. For
example, I would like to get the total number of child nodes from
<parent1and <parent2node. The SelectNodes method return the total
number of <foldernodes (9) regardless of calling from the <parent1>
and <parent2node.

XML:
----------------------------------
<Mailbox DisplayName="test">
<parent1>
<folders>
<folder name="test001">
<folder name="test001"/>
<folder name="test003">
<folder name="test004">
<folder name="test005"/>
<folder name="test006"/>
</folder>
</folder>
</primary>

<parent2>
<folders>
<folder name="test007">
<folder name="test008">
<folder name="test009">
</folders>
</archive>
</Mailbox>
----------------------------------

My C# code return 9 childs for each execution. Correct result will be 6
for <parent1and 3 for the <parent2child node. Here is the code:
----------------------------------
// node = Mailbox node

foreach (XmlNode childNode in node.ChildNodes)
{
XmlNodeList folders = childNode.SelectNodes("//folder");
int total = folders.Count; <---- return 9.
}

----------------------------------

Thanks in advance.

- MA

Nov 28 '06 #1
4 6456
MA
Please ignore the typo of the end tag </archiveand </primary>. Thanks
in advance.

Nov 28 '06 #2
XmlNodeList folders = childNode.SelectNodes("//folder");
MA have u tried childNode.SelectNodes("/Parent/folder"); and
childNode.SelectNodes("/Parent2/folder");
Patrick
"MA" <ma*******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
Please ignore the typo of the end tag </archiveand </primary>. Thanks
in advance.

Nov 28 '06 #3
MA wrote:
foreach (XmlNode childNode in node.ChildNodes)
{
XmlNodeList folders = childNode.SelectNodes("//folder");
You need a relative XPath expression e.g.
childNode.SelectNodes("folder")
for all |folder| element child nodes or
childNode.SelectNodes(".//folder")
for all |folder| element descendant nodes.

If you have an XPath expression starting with / then it is always an
absolute XPath expression starting from the root node so it does not
help with SelectNodes/SelectSingleNode if you want to look at nodes
relative to the node you call the method on.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 28 '06 #4
MA
Thanks "Martin", its working as expected.

Cheers,
MA

Nov 28 '06 #5

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

Similar topics

13
by: kaeli | last post by:
Can anyone explain this to me? It's driving me insane. Save this and run it in IE or Opera and then in Mozilla or Netscape 6+. In IE/Opera, I get the expected 4 alerts. In Mozilla/Netscape, I...
2
by: Bjorn | last post by:
Hi all, Can't seem to figure out how to retrieve the text of a node with multiple child nodes (and I just want the text of the parent, not the InnerText or InnerXML that comes with it). My...
7
by: amruta | last post by:
the code below dows not let me get the parent child view... all the nodes are show in one line only... also i need them to be collasped ... Thanks ..
5
by: B-Dog | last post by:
I want to be able to detect if a user has selected a parent or child node in a treeview and depending on which is clicked act accordingly based on the tag. I can get the values but don't know if...
1
by: SeVeN | last post by:
Hello All, I am having a problem loading nodes and child nodes into a treeview from an access database. My Db fields are as follows : ID NodeName ParentID
2
by: Jack | last post by:
Hello, I am trying use a TreeView with checkboxes. I would like to check more than one node and allow all child nodes of selected nodes to be checked or unchecked with the parent is checked. ...
5
WebDunce
by: WebDunce | last post by:
Hi guys, I am trying to develop a simple xml editor. I have an object that has a TreeView. I use the TreeView to display the Xml node info. That's all fine, but i want the user to be able to move...
2
by: Bob | last post by:
Hi, I would to have a tree view control with check boxes in which the parent and child nodes interacts, in the way that you normally see this occuring. Checking the parent node automatically...
6
by: dav61000 | last post by:
I have a parent form that uses a Treeview Control and I would like to pass the node selected to a child form where the user will add update or delete the record selected using a dataset. Is there a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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
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...

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.