Load a TreeView control from an XML file in C#

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.

Subroutine LoadTreeViewFromXmlFile loads the XML document, clears the TreeView control's nodes, and calls subroutine AddTreeViewChildNodes, passing it the TreeView's Nodes collection and the XML document's root node.

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

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

Subroutine AddTreeViewChildNodes 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 TreeView node to the node collection. It then calls itself recursively to add the XML node's children to the new TreeView 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();
}
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

  • 9/10/2010 7:31 AM Bill Conniff wrote:
    This technique is not memory efficient. The following code builds the entire xml tree in memory:
    xml_doc.Load(filename);
    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:
    http://xponentsoftware.com/BlogComment.aspx?a=article1
    Reply to this
  • 9/10/2010 8:11 AM Rod Stephens wrote:
    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.
    Reply to this
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.