Populating a TreeView Control from XML

Posted Tue, Jul 21 2009 14:58 by Deborah Kurata

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:

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

The TreeView will display the following:

State
--- 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 =
            stateNode.Nodes.Add(region.Attribute("name").Value);
        foreach (XElement area in region.Descendants("Area"))
        {
            regionNode.Nodes.Add(area.Attribute("name").Value);
        }
    }
}

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>
            regionNode.Nodes.Add(area.@name)
        Next
    Next
Next

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).

Enjoy!

Filed under: , , , , ,

Comments

# re: Populating a TreeView Control from XML

Thursday, July 23, 2009 10:51 AM by Star79

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<T>(System.Collections.Generic.IEnumerable<T>)' is a 'method', which is not valid in the given context C:\web_projects\AM\MasterPage.master.cs 32 43 C:\web_projects\AM\

# re: Populating a TreeView Control from XML

Thursday, July 23, 2009 12:42 PM by Deborah Kurata

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.

# re: Populating a TreeView Control from XML

Thursday, July 23, 2009 1:01 PM by Star79

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

Thanks,

appreciate your blog.

# re: Populating a TreeView Control from XML

Monday, August 31, 2009 9:33 AM by Prince.C

Thanks a lot..you saved me..:)

# re: Populating a TreeView Control from XML

Friday, September 11, 2009 4:02 PM by Jeff

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

# re: Populating a TreeView Control from XML

Sunday, October 04, 2009 1:58 PM by Joakim Westin

Thanks for a great little post. Well written and very clear :-)

# re: Populating a TreeView Control from XML

Sunday, October 25, 2009 11:24 PM by ExcelMonkey

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 <State>.  

   stateNode = ?

   For Each region As XElement In state...<Region>

       regionNode = stateNode.Nodes.Add(region.@name)

       For Each area As XElement In region...<Area>

           regionNode.Nodes.Add(area.@name)

       Next

   Next

Thanks

EM

# re: Populating a TreeView Control from XML

Monday, October 26, 2009 1:16 PM by Deborah Kurata

Hi EM -

I assume this thread is answering your question:

social.msdn.microsoft.com/.../6a9507c8-7c37-4850-abd2-c43252a53c33

# Populating a TreeView Control from a List

Monday, November 09, 2009 11:25 PM by Deborah's Developer MindScape

This post details first how to build a list containing the data to display in a WinForms TreeView control

Leave a Comment

(required) 
(required) 
(optional)
(required)