473,382 Members | 1,717 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,382 software developers and data experts.

I can't find EndElement for XML nodes in tree. Help please.

I'm trying to recursively parse a tree of XML nodes. I need to be able to keep track of how deep I am in the tree at any given time. What I had planned to do is use a counter that increments when I move a level deeper into the tree and decrements when I move out of that level. To do this I need to be able to read the start elements and end elements. My code never sees the end elements though. Thanks for your help!
Attached Files
File Type: txt code.txt (2.3 KB, 394 views)
Aug 14 '09 #1
1 2289
GaryTexmo
1,501 Expert 1GB
I don't know what your XML actually looks like, so you might not even see an end element. MSDN looks like it says an EndElement is something like </item> which is for the actual XML. You're using XmlNode, I'm not sure if that would match up.

However, you shouldn't need to know this... you're at the end of a branch of XML when there are no more child nodes. By that I mean, when they are null.

Here's a very simple example of something that will run through an XmlNode, as loaded into an XmlDocument.

Expand|Select|Wrap|Line Numbers
  1. public void TraverseXML(XmlNode rootNode)
  2. {
  3.   // Do something with rootNode
  4.   ...
  5.  
  6.   // Traverse the children
  7.   foreach (XmlNode childNode in rootNode.ChildNodes)
  8.   {
  9.     TraverseXML(childNode);
  10.   }
  11. }
Anything on top of that is just extra (like your counter), that's the core of what you need.

That said, if you want to use the XmlNodeType in the fashion your code is, try using an XML reader instead. A quick google search turned this up...

http://www.xml.com/pub/a/2002/03/06/...ml.html?page=3

Good luck!

*Edit: Oh, one more thing to add. You can always find out how deep an XmlNode is in the hierarchy by traversing up to it's parent. When the parent is null, you're at the top.

Expand|Select|Wrap|Line Numbers
  1. public int LevelsDeep(XmlNode node)
  2. {
  3.   int count = 0;
  4.   XmlNode nextNode = node.ParentNode;
  5.   while (nextNode != null)
  6.   {
  7.     count++;
  8.     node = node.ParentNode;
  9.   }
  10.  
  11.   return count;
  12. }
I think there might be one extra level at the top that's the document element, but don't quote me 100% on that since I'm digging it out of memory. It doesn't take too long to try it out and see.
Aug 14 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mr Chat | last post by:
Hello all I am trying to write a specialist outlining tool, using the TreeView control (in VB5 running on W95). It has gone quite well so far. I have found some very useful info on how to...
4
by: abhrajit | last post by:
I'm looking for a C/C++/Java library to create a balanced binary tree data structure given a set of leaf nodes as input. A leaf node should never become an interior node. So if I wish to create...
4
by: pmcguire | last post by:
I have a treeview with a lot of nodes. I want to load only the nodes that are initially visible when the form loads, and then continue to populate it in background and/or when the nodes are required...
3
by: Sergio Terenas | last post by:
Hi all, I've a Treeview control in a form load with 600 nodes. Each node has a text and tag associated to it at the time I add it. I need to find a node by either the text or the tag, make it...
4
by: Ganesh Muthuvelu | last post by:
Hi STAN, Stan: Thanks for your response to my previous post on reading a XSD file using your article in "https://blogs.msdn.com/stan_kitsis/archive/2005/08/06/448572.aspx". it works quite well...
4
by: Gregory Piñero | last post by:
Hi, Would anyone be able to tell me why my function below is getting stuck in infinite recusion? Maybe I'm just tired and missing something obvious? def...
5
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method...
10
by: John Rogers | last post by:
This code only counts the parent nodes or rootnodes in a treeview, how do you count all the nodes in a treeview? // one way int NodeCounter = 0; foreach (TreeNode currentNode in...
6
Sl1ver
by: Sl1ver | last post by:
I've got a problem, i got the nodes to move but 1. they copy nodes(if you drag it to 3 different places it will actually have 3 of the same nodes) 2. i want to make the nodes, if moved update the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.