Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

July 21, 2009

Populating a TreeView Control from XML

Filed under: C#,VB.NET,WinForms,XML @ 4:58 pm

This post describes how to populate a WinForms TreeView control from an XML file assuming you are targeting the .NET Framework Version 3.5.

The XML file used in this example looks like this:

  <State name="California">
      <Region name="San Luis Obispo">
        <Area name="Santa Maria" />
        <Area name="Seaside" />
      <Region name="Silicon Valley">
        <Area name="San Jose"/>
        <Area name="Sunnyvale"/>
  <State name="Wisconsin">
      <Region name="Milwaukee">
        <Area name ="Mukwanago"/>
        <Area name="Germantown"/>
      <Region name="Fox Valley">
        <Area name="Oshkosh" />
        <Area name="Appleton" />

The TreeView will display the following:

— Region
—— Area

The code is provided here in VB and C# and then described in detail below.

NOTE: Be sure to set a reference to System.Core and System.Xml.Linq

In C#:

XElement doc  = XElement.Load("testXML.xml");

TreeNode stateNode;
TreeNode regionNode;
foreach (XElement state  in doc.Descendants("State"))
    stateNode = treeView1.Nodes.Add(state.Attribute("name").Value);
    foreach (XElement region in state.Descendants("Region"))
        regionNode =
        foreach (XElement area in region.Descendants("Area"))

In VB:

Dim doc As XElement = XElement.Load("testXML.xml")

Dim stateNode As TreeNode
Dim regionNode As TreeNode
For Each state As XElement In doc…<State>
    stateNode = TreeView1.Nodes.Add(state.@name)
    For Each region As XElement In state…<Region>
        regionNode = stateNode.Nodes.Add(region.@name)
        For Each area As XElement In region…<Area>

In both cases, the XML is first retrieved from a file. An in-memory XML string could be used instead.

Three for/each loops are used, one for each level of the TreeView hierarchy.

The states are processed first. A node is added for each state name.

The regions are processed next. A node is added under the state for each region name.

Finally, the areas are processed. A node is added under the region for each area name.

Notice that the VB code leverages XML literals. The C# code uses XElements (which also work in VB).



  1.   Star79 — July 23, 2009 @ 10:51 am    Reply

    Get the following errors from the code:

    Error 1 The best overloaded method match for ‘System.Web.UI.WebControls.TreeNodeCollection.Add(System.Web.UI.WebControls.TreeNode)’ has some invalid arguments C:\web_projects\AM\MasterPage.master.cs 29 29 C:\web_projects\AM\

    Error 2 Argument ‘1’: cannot convert from ‘string’ to ‘System.Web.UI.WebControls.TreeNode’ C:\web_projects\AM\MasterPage.master.cs 29 52 C:\web_projects\AM\

    Error 3 ‘System.Xml.Linq.Extensions.Nodes(System.Collections.Generic.IEnumerable)’ is a ‘method’, which is not valid in the given context C:\web_projects\AM\MasterPage.master.cs 32 43 C:\web_projects\AM\

  2.   DeborahK — July 23, 2009 @ 12:42 pm    Reply

    Hi Star79 –

    Thank you for stopping by the blog. It looks like from your errors you are using a WebForms TreeView?

    This code is for a WinForms TreeView as per the first line of the post.

    You can use the same general concept for accessing the XML, but you would need to change the code to add nodes to the WebForms TreeView to the correct ASP.NET syntax.

  3.   Star79 — July 23, 2009 @ 1:01 pm    Reply

    Hi Thanks for pointing that out…Iam totally new to LINQ can you pls guide me on this..
    appreciate your blog.

  4.   Prince.C — August 31, 2009 @ 9:33 am    Reply

    Thanks a saved me..:)

  5.   Jeff — September 11, 2009 @ 4:02 pm    Reply

    Very straight forward and it helped a lot.– thanks for posting it!

  6.   Joakim Westin — October 4, 2009 @ 1:58 pm    Reply

    Thanks for a great little post. Well written and very clear 🙂

  7.   ExcelMonkey — October 25, 2009 @ 11:24 pm    Reply

    Deborah, if you only wanted to populate the California portion of the XML tree how would you do this? I am assuming that you have to take out the first For/Next stmt with .

    stateNode = ?
    For Each region As XElement In state…
    regionNode = stateNode.Nodes.Add(region.@name)
    For Each area As XElement In region…




  8.   DeborahK — October 26, 2009 @ 1:16 pm    Reply

    Hi EM –

    I assume this thread is answering your question:

  9.   Stephan Kamstra — July 30, 2011 @ 5:53 am    Reply

    How can I export the same structure (like your example of the xml) from the treeview back into the xml file?


  10.   Morteza karimian — December 19, 2012 @ 12:28 pm    Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2022 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs