C# TreeView读取Xml

C#  TreeView读取Xml

1.    Xml文本内容如下:



	
		CEO
	
	
		CTO
	
	
		CFO
	
	
		Engineer
	

2.    效果图如下:

C# TreeView读取Xml_第1张图片

3.    按钮点击加载xml文件

private void button2_Click(object sender, EventArgs e)
    {
        XmlDocument docEmp = new XmlDocument();
        docEmp.Load("EmpXml.xml");//文件放在debug里,
        XmlNode rootNode = docEmp.SelectSingleNode("xml");
        XmlNodeList firstLvelNodeList = rootNode.ChildNodes;

        readEmpXml(firstLvelNodeList, treeView2.Nodes);
        treeView2.ExpandAll();
    }

4.    按序添加节点

private void readEmpXml(XmlNodeList xmlNodeList, TreeNodeCollection nodes)
        {
            foreach (XmlNode node in xmlNodeList)
            {
                string id = node.Attributes["id"].Value;
                string p_id = node.Attributes["p_id"].Value;

                TreeNode tNode = new TreeNode();
                tNode.Text = node.InnerText;
                tNode.Name = node.Attributes["id"].Value;
                TreeNode newNode;
                if (p_id == "")
                    nodes.Add(tNode);
                else
                {
                    IdfindNode(p_id,treeView2.Nodes, out newNode);
                    newNode.Nodes.Add(tNode);
                }
            }
        }

5.    查找节点

private void IdfindNode(string pid, TreeNodeCollection Nodes, out TreeNode newNode)
        {
            newNode = null;
            foreach (TreeNode node in Nodes)
            {
                if (node.Name.Equals(pid))
                {
                    newNode = node;
                    break;
                }
                else
                    if (node.Nodes.Count != 0)
                        IdfindNode(pid,node.Nodes, out newNode);
            }
        }

6.    Xml读取至TreeView完整代码如下:

private void button2_Click(object sender, EventArgs e)
        {
            XmlDocument docEmp = new XmlDocument();
            docEmp.Load("EmpXml.xml");//xml放到debug里
            XmlNode rootNode = docEmp.SelectSingleNode("xml");
            XmlNodeList firstLvelNodeList = rootNode.ChildNodes;

            readEmpXml(firstLvelNodeList, treeView2.Nodes);
            treeView2.ExpandAll();
        }
        private void readEmpXml(XmlNodeList xmlNodeList, TreeNodeCollection nodes)
        {
            foreach (XmlNode node in xmlNodeList)
            {
                string id = node.Attributes["id"].Value;
                string p_id = node.Attributes["p_id"].Value;

                TreeNode tNode = new TreeNode();
                tNode.Text = node.InnerText;
                tNode.Name = node.Attributes["id"].Value;
                TreeNode newNode;
                if (p_id == "")
                    nodes.Add(tNode);
                else
                {
                    IdfindNode(p_id,treeView2.Nodes, out newNode);
                    newNode.Nodes.Add(tNode);
                }
            }
        }
        private void IdfindNode(string pid, TreeNodeCollection Nodes, out TreeNode newNode)
        {
            newNode = null;
            foreach (TreeNode node in Nodes)
            {
                if (node.Name.Equals(pid))
                {
                    newNode = node;
                    break;
                }
                else
                    if (node.Nodes.Count != 0)
                        IdfindNode(pid,node.Nodes, out newNode);
            }
        }

 

 

你可能感兴趣的:(C#,c#,treeview,xml)