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

Traversing XML tree - but how do I get the leafs only?

Good Day, folks!

I'm having a problem traversing an XmlDocument tree in C#. I only want
to access the InnerText of the leafs and the names of their ancestors
and show them in a richTextBox. But if I go like this I get the
InnerText of all the nodes and the outcome in my richTextBox is just a
mess.

The XML files i want to handle here have about 2300 nodes, so that
accessing some nodes directly is just impossible.

Can anyone help me here? Thanks!

BTW: What is this CreateNavigator() thing doing? I just found that
somewhere but haven't found out what I can do with it yet.

/// <summary>
/// writes collected data to page
/// </summary>
public void printPage(XmlDocument dom)
{
richTextBox1.Text = null;

// node-tree root
XmlNode root = dom.DocumentElement;

//nodeRead = new XmlNodeReader(dom);
//nav = dom.CreateNavigator();

// SELECT statement
XmlNodeList nodeList;
nodeList = root.SelectNodes("/descendant::*");

MessageBox.Show(Convert.ToString(nodeList.Count));

// traverse the node-tree
if(dom.HasChildNodes)
{
foreach(XmlNode myNode in nodeList)
{
richTextBox1.Text = richTextBox1.Text
+ Convert.ToString(myNode.Name) + ": "
/*+ Convert.ToString(myNode.InnerText)*/
+ "\n\n";
}
}
}

Aug 15 '06 #1
4 5464


Christian Rühl wrote:

I'm having a problem traversing an XmlDocument tree in C#. I only want
to access the InnerText of the leafs and the names of their ancestors
and show them in a richTextBox. But if I go like this I get the
InnerText of all the nodes and the outcome in my richTextBox is just a
mess.
public void printPage(XmlDocument dom)
{
richTextBox1.Text = null;
XmlNodeList nodeList;
nodeList = root.SelectNodes("/descendant::*");
Use
XmlNodeList nodeList = dom.SelectNodes("//*[not(*) and text()]");
foreach (XmlElement element in nodeList) {
// use e.g. element.InnerText, element.Name
}
That way you process all elements that have no elements as child nodes
but have text child nodes.

The real leaf nodes however are the text nodes so depending on your
needs you might want
XmlNodeList nodeList = dom.SelectNodes("//text()");
and loop through those text nodes.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 15 '06 #2
cool, cool! thank you martin! that was exactly what i was looking for!
thanks!!!

Aug 16 '06 #3
okay guy, here's another question for you now:

i built an iterator to get all the nodes with a certain attribute in an
xml dom. this all looks like this:

string xpath;

// iterate over all xmlnodes with attribute "status='checked'"
XPathDocument xpathDoc = new XPathDocument(m_configFile);
XPathNavigator xpathNav = xpathDoc.CreateNavigator();
XPathNodeIterator nodeIter = xpathNav.Select(@"/*[@status='checked']");

while(nodeIter.MoveNext())
{
xpath = nodeIter.CurrentPosition.ToString();
// here i want to address an treeview node with the xpath to set its
checkbox
}

so, how can i address a treeview node with a path? for if i go like
treeView1.SelectedNode it randomly choses a node in treeView control.

i'm using .NET framework 1.1.

Aug 21 '06 #4
hello!

i hope qou can help me with another problem i have.
what i want to do is simple: i have an xpath of a node that not yet
exists and i want to create that node in my config.xml file.

to do this, i split up the xpath into parts and check if a
correspondent node exists by putting fragment by fragment together
until i hit a part of my path that does not exist.

if i suffer such an event, i want to add a child node to the existing
parent node and so on... i don't get errors, but somehow this doesn't
work. can anybody help me out? i am really grounded now... i absolutly
no idea how to solve this. i have a feeling, that i'm pretty close now,
but i can't do the last steps...

thanks in advance!

/// <summary>
/// add child nodes by xpath
/// </summary>
private XmlNode AddNodeByXpath(string xpath, XmlDocument dom)
{
XmlNode node;
XmlNode parent;

string[] tempXpath = null; // empty array
char separator = Convert.ToChar("/"); // separator sign

string temp = null; // empty temp node
string oldTemp = null;
int i = 1; // split index

try
{
node = dom.SelectSingleNode(xpath);

// Get a XML node type object.
Type XmlNodeType = typeof(XmlNode);

// split xpath into parts
tempXpath = xpath.Split(separator);

// set temp node = root
temp = tempXpath[0];
oldTemp = temp;

// node checked and correspondent xml node found?
while(i<tempXpath.Length)
{
node = dom.SelectSingleNode(temp);
parent = dom.SelectSingleNode(oldTemp);

if(XmlNodeType.IsInstanceOfType(node))
{
// if node exists, check for child node
oldTemp = temp;
temp = temp + "/" + tempXpath[i];
}
else
{
//node = dom.CreateElement(tempXpath[i]);
// if node does not exist, append node
parent.AppendChild(dom.CreateElement(tempXpath[i]));
}

// perform step
i++;
}
return node;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}

Aug 29 '06 #5

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

Similar topics

2
by: C-man | last post by:
Yeah, for some reason I can seem to find an algorithm that will simple but items into the tree in fashion such that a new leaf can't be added to a new level till all the leafs at the previous level...
2
by: VB | last post by:
Hi, I'm looking for a javascript that produces a dynamic tree like in www.vivisimo.com or in http://carrot.cs.put.poznan.pl/carrot2-remote-controller/index.jsp In particular, i need that in...
0
by: perchef | last post by:
Hello, I'm looking for a way to select some leafs in a tree. My idea is to use a TreeListCtrl, with the tree in the first column and checkboxes in the second. But I don't know how to do this,...
1
by: alfred | last post by:
Hi my question is on traversing a tree with DOM. how would I be able to traverse 2 trees at the same time. I have 2 XML documents, with similar nodes. I would like to traverse 1 xml document,...
4
by: A_StClaire_ | last post by:
hi all, I'm trying to implement Huffman coding on letters of a string. I've written functions to define the initial leaf nodes (letters and their frequencies) and to sort them. I've also put...
3
by: mikelbell2000 | last post by:
I am attempting to write a query that will traverse the relationships for tables in my database that will provide me the order in which I need to perform delete operations to avoid running into...
2
by: Defected | last post by:
Hi, How i can implement a main function with this Binary Search Tree. thanks for help. is this code corrected ? #include<iostream>
1
by: ericstein81 | last post by:
I need to traverse a binary search tree and be able to give an output of how many leaf nodes there are, and how many leafs have one child and how many leafs have two children. Im not asking for a...
0
by: mac | last post by:
I found that with memory allocating techniques used nowadays (addresses alignment, eg. on 32bit machines) one can detect loops in a tree structure very fast, without using extra memory. This is due...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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.