Load a TreeView control from an XML file in C#


This example shows how to load a TreeView control with XML data. An XML (eXtensible Markup Language) file is a simple file that stores tokens in a hierarchical manner. It doesn’t do anything except hold data. Programs can load and manipulate the data. Because XML files hold hierarchical data, it makes sense to use them to hold data for a TreeView control, which displays hierarchical data.

The following LoadTreeViewFromXmlFile method starts the loading process.

// Load a TreeView control from an XML file.
private void LoadTreeViewFromXmlFile(string filename, TreeView trv)
    // Load the XML document.
    XmlDocument xml_doc = new XmlDocument();

    // Add the root node's children to the TreeView.
    AddTreeViewChildNodes(trv.Nodes, xml_doc.DocumentElement);

This method loads the XML document, clears the TreeView control’s nodes, and calls the following AddTreeViewChildNodes method to load the data, passing it the TreeView‘s Nodes collection and the XML document’s root node.

// Add the children of this XML node 
// to this child nodes collection.
private void AddTreeViewChildNodes(
    TreeNodeCollection parent_nodes, XmlNode xml_node)
    foreach (XmlNode child_node in xml_node.ChildNodes)
        // Make the new TreeView node.
        TreeNode new_node = parent_nodes.Add(child_node.Name);

        // Recursively make this node's descendants.
        AddTreeViewChildNodes(new_node.Nodes, child_node);

        // If this is a leaf node, make sure it's visible.
        if (new_node.Nodes.Count == 0) new_node.EnsureVisible();

The AddTreeViewChildNodes method takes as parameters a TreeNodeCollection that should contain the child nodes defined by an XML node, and an XML node. For each of the node’s children, the subroutine adds a new TreeNode to the node collection. The method then calls itself recursively to add the XML node’s children to the new TreeNode.

When it finishes, the method calls the new node’s EnsureVisible method to make sure it is expanded. It only calls EnsureVisible for leaf nodes (nodes without children). That will make the control expand all of the nodes leading to the leaf, so every node is visible. (You can omit this part if you prefer.)

Download Example   Follow me on Twitter   RSS feed

This entry was posted in controls, XML and tagged , , , , , , , , , . Bookmark the permalink.

3 Responses to Load a TreeView control from an XML file in C#

  1. Bill Conniff says:

    This technique is not memory efficient. The following code builds the entire xml tree in memory:
    and then has to load the document child nodes into a treeview.
    Most XML viewers/editors eliminate the first step by using an XML parser, like XmlReader, to add just the child nodes under the root to the treeview control. That is much more memory efficent, but can still run into memory and performance issues if the XML is really huge with tens of thousands of document child nodes.

    See my article for ideas on a more effient strategy:

  2. Rod Stephens says:

    Yes, that’s true. XmlReader uses less memory. But if you’re loading tens of thousands of items, you may have trouble with the TreeView control, too.

    XmlReader is a much better approach if you need to read a huge XML document but only need to actually load a small fraction of the data. For example, suppose the XML file has 10,000 entries but you only need to load 100 into the TreeView. In that case, XmlReader can sift through them one at a time and discard those that you don’t need without loading everything at once.

  3. Pingback: Load a WPF TreeView control from an XML file in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *