读取XML数据到treeView中

/// <summary>
        ///   绑定xml数据到treeview中区
        /// </summary>
        public void bindXmlToTreeView(XmlNodeList nodeList,TreeNodeCollection treeNode)
        {
       
            foreach (XmlNode node in nodeList)
            {
                XmlElement xe = (XmlElement)node;
                TreeNode newTreeNode = new TreeNode();
                newTreeNode.Text = xe.GetAttribute("name");
                newTreeNode.Name = xe.GetAttribute("value");
                treeNode.Add(newTreeNode);
               
                if (node.HasChildNodes)
                {
                    bindXmlToTreeView(node.ChildNodes,newTreeNode.Nodes);
                }
            }        }
 
 

 

使用:

 

复制代码
XmlDocument xmlDoc = new XmlDocument();//
XmlNodeList currentXmlNodeList = null;   

xmlDoc.Load(baseDir);

currentXmlNodeList = xmlDoc.SelectNodes("/root/GasTree");

bindXmlToTreeView(currentXmlNodeList,treeView1.Nodes);

复制代码

你可能感兴趣的:(treeview)