Click here to Skip to main content
15,500,961 members
Articles / Web Development / ASP.NET
Posted 10 Sep 2009


39 bookmarked

Populate TreeView Menu with XML

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
10 Sep 2009CPOL1 min read
This step-by step article describes how to populate a TreeView control by using XML data.


Creating treeview menus on the fly from an XML file can be useful when the menu items are constantly being updated. For instance, when using an XML file as a database to store records in.

Here is a simple example of how to do this. The example is kept simple to avoid any confusion.


A working knowledge of XML, the TreeView control, and the Visual Studio is helpful in understanding the steps.

Using the code

Note: The attached solution file (xml2treeviewmenuSolution) was created with VS2010.

Add an XML file to your project and name it "menu.xml". Edit the XML file with the menu items.

<?xml version="1.0" encoding="utf-8" ?>
<folder title='folder 1a' >
<record title='record 1a1' />
<record title='record 1a2' />
<folder title='folder 1b'>
<record title='record 1b1' />
<folder title='folder 2a' >
<record title='record 2a1' />
<folder title='folder 3a' >
<record title='record 3a1' />
<record title='record 3a2' />

Drag the TreeView control from the Visual Studio Toolbox onto your Windows form. In this example, I named the control "treeViewMenu".

Add references to the XML classes in your using statements.

using System.Xml;
using System.Xml.XPath;    

Create an XML document to hold the file.

public partial class Form1 : Form
    private XmlDocument docXML = new XmlDocument();

When the form is loaded, load the XML document with the XML file and begin populating the TreeView control.

private void Form1_Load(object sender, EventArgs e)
    docXML.Load("menu.xml"); // Load the xml file
    populateBaseNodes(); // Populate all of the base nodes

Population begins with the first level <folder> nodes. After each base node is added to the tree, the child nodes for the current base node are added.

private void populateBaseNodes()
    treeViewMenu.Nodes.Clear(); // Clear any existing items
    treeViewMenu.BeginUpdate(); // Begin updating the treeview
    TreeNode treenode;
    treenode = treeViewMenu.Nodes.Add("Folders");
    XmlNodeList baseNodeList = docXML.SelectNodes("root/folder");
    // Get all first level <folder> nodes

    foreach (XmlNode xmlnode in baseNodeList)
    // loop through all base <folder> nodes 
        string title = xmlnode.Attributes["title"].Value;

        treenode = treeViewMenu.Nodes.Add(title); // add it to the tree

        populateChildNodes(xmlnode, treenode); // Get the children

    treeViewMenu.EndUpdate(); // Stop updating the tree
    treeViewMenu.Refresh(); // refresh the treeview display

Each child node will be inspected for further children. The loop will called for each child node that was found.

private void populateChildNodes(XmlNode oldXmlnode, TreeNode oldTreenode)
    TreeNode treenode = null;
    XmlNodeList childNodeList = oldXmlnode.ChildNodes;
    // Get all children for the past node (parent)

    foreach (XmlNode xmlnode in childNodeList)
    // loop through all children
        string title = xmlnode.Attributes["title"].Value;
        // add it to the parent node tree
        treenode = oldTreenode.Nodes.Add(title);
        populateChildNodes(xmlnode, treenode); 

Points of interest

Fancy up your menu using an image list and add an icon attribute to your menu file.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Germany Germany
I am an american living in Germany.

I've been professionaly engaged in computer technology and programming for over 25 years.

Comments and Discussions

QuestionTreeView isn't showing. Pin
Member 1189322018-Aug-15 3:56
MemberMember 1189322018-Aug-15 3:56 
QuestionHow to do this in MFC (C++) VS 2010 ? Pin
Michael B Pliam20-Oct-13 20:27
MemberMichael B Pliam20-Oct-13 20:27 
Questionplease read Pin
ahmed youness1115-Sep-12 4:35
Memberahmed youness1115-Sep-12 4:35 
QuestionDon't Work Please Pin
ahmed youness1128-Aug-12 6:57
Memberahmed youness1128-Aug-12 6:57 
GeneralRe: Don't Work Please Pin
EgyptianRobot28-Aug-12 7:28
MemberEgyptianRobot28-Aug-12 7:28 
GeneralRe: Don't Work Please Pin
ahmed youness111-Sep-12 17:09
Memberahmed youness111-Sep-12 17:09 
GeneralRe: Don't Work Please Pin
EgyptianRobot2-Sep-12 2:06
MemberEgyptianRobot2-Sep-12 2:06 
GeneralRe: Don't Work Please Pin
ahmed youness119-Sep-12 9:46
Memberahmed youness119-Sep-12 9:46 
GeneralRe: Don't Work Please Pin
EgyptianRobot9-Sep-12 10:43
MemberEgyptianRobot9-Sep-12 10:43 
GeneralRe: Don't Work Please Pin
ahmed youness1111-Sep-12 15:44
Memberahmed youness1111-Sep-12 15:44 
AnswerRe: Don't Work Please Pin
ralph195710-Sep-12 4:23
Memberralph195710-Sep-12 4:23 
GeneralRe: Don't Work Please Pin
ahmed youness1112-Sep-12 7:53
Memberahmed youness1112-Sep-12 7:53 
GeneralRe: Don't Work Please Pin
ralph195712-Sep-12 8:58
Memberralph195712-Sep-12 8:58 
GeneralRe: Don't Work Please Pin
ahmed youness1112-Sep-12 11:40
Memberahmed youness1112-Sep-12 11:40 
I Make my applciation name like yours and copy all code in the file it works when i write it it dont work
GeneralRe: Don't Work Please Pin
ahmed youness1112-Sep-12 12:38
Memberahmed youness1112-Sep-12 12:38 
GeneralThanks! my vote 5! Pin
GrG9-May-12 9:31
MemberGrG9-May-12 9:31 
GeneralMy vote of 5 Pin
winuk14-Dec-11 2:35
Memberwinuk14-Dec-11 2:35 
GeneralThanks Pin
jamesyla2-Nov-09 19:11
Memberjamesyla2-Nov-09 19:11 
GeneralMy vote of 1 Pin
Manas Bhardwaj11-Sep-09 1:20
professionalManas Bhardwaj11-Sep-09 1:20 
GeneralRe: My vote of 1 Pin
Anthony Daly13-Dec-09 2:50
MemberAnthony Daly13-Dec-09 2:50 
GeneralRe: My vote of 1 Pin
jyothi_m0331-Aug-12 19:54
Memberjyothi_m0331-Aug-12 19:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.