代码如下

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

 

posted on 2011-12-15 10:47  jackdesk  阅读(3175)  评论(0编辑  收藏  举报