• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

Need more explaination on Treeviews

I need more explanation on the treeview.
How can I select a Nodes?

Example :


I would like List each element of the treeview and i want to be able to insert a level in a node.

Ex: Level1_0_0


  • 3
  • 3
  • 3
1 Solution
I'm not entirely sure what you mean, but I have a resusable routine to load a treeview from an XML document and control the nodes from there, using XML attributes to provide text, image color etc. properties for the node and tying an id attribute to the node key. This has several advantages mainly because you can store additional node information in the XML, and best of all use XPath to get any subsets of nodes, counts of nodes ect. XPath makes it easy to find things like 'Level1_0_0' with out recursing through all the nodes.
Manipulating all the data in the treeview itself, whilst completely possible, usually turns out very messy with lots of recursive routines to iterate through the nodes. The XML document provides all the methods you need for adding, moving ,removing and searching elements, which can be easily mapped to nodes. It seems like more at first, but by the second or third time you use it you realise how much easier it is.
If you are not familiar with XML and XPath it is well worth the effort.
Which treeview control did you use?
As far as I know, treeview control is not currently supported.

"This product is only compatible with machines running builds of the .NET Framework (1.0.3705.0) and Visual Studio® .NET (7.0.9466)."

This can be read here(where you can download the treeview too):


DrChalAuthor Commented:
Thanks for your explanation.
But i'm a beginner in C#. Have you got an example of code for filling a treeview with XML File.


Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

ibai, I'm just using the standard System.Windows.Form.TreeView

okay here's an example:
This uses a saved xml file ("c:/xmlfile1.xml") for practical use you'll probably want to build one on the fly, (this isn't the most straight forward thing to as the xml document has long winded ways of adding nodes and attributes, however it is easy to write very simple wrapper functions to do these for you and these will serve you well for many things)

Tree loading code:

          private void LoadTree()

               XmlDocument xmldoc =new XmlDocument();
               xmldoc.Load ("c:/xmlfile1.xml");

          private void RecurseLoadNode(TreeNodeCollection tnodes,XmlNode xmlnode)
               foreach (XmlNode childnode in xmlnode.ChildNodes)
                    TreeNode tn =tnodes.Add(childnode.Attributes.GetNamedItem("text").Value);
                    if (childnode.ChildNodes.Count > 0)

with this xml document:

<?xml version="1.0" encoding="utf-8" ?>
     <treenode id="a" text="firstnode">
          <treenode id="b" text="firstnodefirstchild"></treenode>
          <treenode id="c" text="firstnodesecondchild">
               <treenode id="n" text="firstnodesecondchildgrandchild1"></treenode>
               <treenode id="o" text="firstnodesecondchildgrandchild2"></treenode>
               <treenode id="p" text="firstnodesecondchildgrandchild3"></treenode>
          <treenode id="d" text="firstnodethirdchild"></treenode>
          <treenode id="e" text="firstnodeforthchild"></treenode>
          <treenode id="f" text="firstnodefifthchild"></treenode>
     <treenode id="g" text="secondnode">
          <treenode id="h" text="secondnodesecondchild"></treenode>
          <treenode id="i" text="secondnodesecondchild"></treenode>
          <treenode id="j" text="secondnodethirdchild"></treenode>
          <treenode id="k" text="secondnodeforthchild">
               <treenode id="q" text="secondnodeforthchildgrandchild1"></treenode>
               <treenode id="r" text="secondnodeforthchildgrandchild2"></treenode>
               <treenode id="s" text="secondnodeforthchildgrandchild3"></treenode>
          <treenode id="l" text="secondnodefifthchild"></treenode>
          <treenode id="m" text="secondnodesixthchild"></treenode>

This does have a learning overhead of needing to understand the XML document model, but you'll probably want to learn this anyway.


Sorry, I was thinking about ASP.NET treeview.
Did you know any control like it for ASP.NET?
As I said before, the MS treeview is not supported more.
Any idea?
Ibai, I've not had anything to do with ASP.NET yet, but I have written an XSLT to convert a tree XML as above to HTML/Javascript that will display a tree in a browser, expand/collapse, highlight and detect clicks as you would expect. Its  a bit much to explain here, but if you're familiar with XSL well worth looking into.
Could you send me the xslt please?
my email is igarle#@#hotmail.com
without # of course
DrChalAuthor Commented:
Thank you ReAdm,
With this example, i can do that i want to make.

DrChalAuthor Commented:
Can I ask you another question?
How have you built your Xlm File?
Do you use this syntax :
DataSet DsXml;
FileStream file=new FileStream("resultat.xml", FileMode.OpenOrCreate, FileAccess.Write);
//Write File              
DsXml.WriteXml (file);


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now