开发者

populate a tree view with an xml file using xElement using c#

开发者 https://www.devze.com 2022-12-29 23:34 出处:网络
Im using 开发者_如何学Goc#.net windows form application. I have a xml file .I need to populate this xml file into a tree view. I can do that using \"xml document\" but i need to do it using xElement.

Im using 开发者_如何学Goc#.net windows form application. I have a xml file .I need to populate this xml file into a tree view. I can do that using "xml document" but i need to do it using xElement.


Please refer to the article Populating a TreeView Control from XML, which says

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

        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);
                }
            }
        }


The following Class loads your .xml into an XElement collection then iterates over it, adding nodes according to their position in the original document and using XML node attributes for tree node naming:

using System.Windows.Forms;
using System.Xml.Linq;

namespace _Solution.Class
{
    class Load
    {
        public void tree_pop(TreeView treeview)
        {
            treeview.Nodes.Clear();
            XElement doc = XElement.Load("xml_file.xml");
            TreeNode root = new TreeNode("root");
            treeview.Nodes.Add(root);
            add_nodes(root, doc);
            treeview.ExpandAll();
        }

        private void add_nodes(TreeNode t_node, XElement x_node)
        {
            foreach (XElement node in x_node.Elements())
            {
                TreeNode n_node = t_node.Nodes.Add(node.Attribute("name").Value);
                add_nodes(n_node, node);
                if (n_node.Nodes.Count == 0) n_node.EnsureVisible();
            }
        }
    }
}

Instantiate and call as follows:

    Load load = new Load();
    private void Form1_Load(object sender, EventArgs e)
    {
        load.tree_pop(treeview);
    }

What's happening in tree_pop is that you're clearing your treeview of any nodes that may already exist, loading your .xml document into an XElement collection, establishing your treeview's root node, calling add_nodes to iterate over the XElement collection then expanding the treeview nodes.

What's happening in add_nodes is that you're iterating over the XElement collection as passed from tree_pop, creating and adding a new node to the treeview then searching each XElement for children and, if found, adding those to the node you just added to the tree.

For copy / paste, remember to adjust your Class namespace to match that of your solution.


Try the following:

var data = XElement.Load(source.xml);
foreach (XElement single in data.Elements())
{
  treeNode.Nodes.Add(single.Element("name of element").Value);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消