您的位置:首页 > 编程语言 > C#

C# xml内容绑定treeview

2016-06-27 09:46 295 查看
        private void button1_Click(object sender, EventArgs e)

        {

            string filepath=Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory )+"\\11.xml";

            if (File.Exists(filepath ))

            {

                XmlDocument xld=new XmlDocument() ;

                xld.Load(filepath );

                RecursionTreeControl(xld.DocumentElement, treeView1.Nodes);

                treeView1.ExpandAll();         //展开TreeView控件中的所有项

            }

        }

 private void RecursionTreeControl(XmlNode xmlNode, TreeNodeCollection nodes)

        {

            foreach (XmlNode node in xmlNode.ChildNodes)        //循环遍历当前元素的子元素集合

            {

                string temp = (node.Value != null ? node.Value : (node.Attributes != null && node.Attributes.Count > 0) ? node.Attributes[0].Value : node.Name);          

                TreeNode new_child = new TreeNode(temp);            //定义一个TreeNode节点对象

                nodes.Add(new_child);         //向当前TreeNodeCollection集合中添加当前节点

                RecursionTreeControl(node, new_child.Nodes);   //调用本方法进行递归

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: