• TreeView绑定XML文档


       

     #region  绑定TreeView控件
        
    /// <summary>
        
    /// 绑定TreeView控件
        
    /// </summary>
        
    /// <param name="treeview">TreeView控件</param>
        
    /// <param name="xpath">查询条件</param>

        protected void BindTreeView(TreeView treeview, string xpath)
        
    {
            
    //声明XML数据源,供绑定TreeView控件时使用
            XmlDataSource datasource = new XmlDataSource();
            datasource.DataFile 
    = Server.MapPath("."+ "/TreeMenu/TreeMenu.xml";
            
    if (xpath == "")
            
    {
                datasource.XPath 
    = "/*/*";
            }

            
    else
            
    {
                datasource.XPath 
    = xpath;
            }

            
    //添加TreeNodeBinding控件集合
            TreeNodeBinding binding1 = new TreeNodeBinding();
            binding1.DataMember 
    = "TreeNodes";
            treeview.DataBindings.Add(binding1);

            TreeNodeBinding binding2 
    = new TreeNodeBinding();
            binding2.DataMember 
    = "TreeNode";
            binding2.TextField 
    = "Text";
            binding2.NavigateUrlField 
    = "URL";
            treeview.DataBindings.Add(binding2);

            TreeNodeBinding binding3 
    = new TreeNodeBinding();
            binding3.DataMember 
    = "Nodes";
            binding3.TextField 
    = "Text";
            binding3.NavigateUrlField 
    = "URL";
            treeview.DataBindings.Add(binding3);

            TreeNodeBinding binding4 
    = new TreeNodeBinding();
            binding4.DataMember 
    = "Node";
            binding4.TextField 
    = "Text";
            binding4.NavigateUrlField 
    = "URL";
            treeview.DataBindings.Add(binding4);
            
    //为TreeView控件的根目录绑定值
            treeview.DataBindings[0].DataMember = "TreeNodes";
            treeview.DataBindings[
    0].Text = "秦山三期OA系统";
            
    //为父级目录绑定值
            treeview.DataBindings[1].DataMember = "TreeNode";
            treeview.DataBindings[
    1].TextField = "Text";
            treeview.DataBindings[
    1].NavigateUrl = "URL";
            
    //为1级节点绑定值
            treeview.DataBindings[2].DataMember = "Nodes";
            treeview.DataBindings[
    2].TextField = "Text";
            treeview.DataBindings[
    2].NavigateUrlField = "URL";
            
    //为2级节点绑定值
            treeview.DataBindings[3].DataMember = "Node";
            treeview.DataBindings[
    3].TextField = "Text";
            treeview.DataBindings[
    3].NavigateUrlField = "URL";
            
    //绑定TreeView控件
            treeview.DataSource = datasource;
            treeview.DataBind();
        }

        
    #endregion


    上面蓝色字体是关键,主要用到了树属性中的XPath对显示条件进行筛选.从XML文档的第二级节点开始显示.

    绑定TreeView过程中还可以用循环方式添加节点到树中.方法如下:

      

      private void ShowElement()
        
    {
            
    string xmlpath = Server.MapPath("."+ "/TreeMenu/TreeMenu.xml";
            XmlDocument xmlDoc 
    = new XmlDocument();
            xmlDoc.Load(xmlpath);
            XmlNodeList nodeList 
    = xmlDoc.SelectSingleNode("TreeNodes").ChildNodes;//获取TreeNodes节点的所有子节点
            
    //定义三个节点
            TreeNode treenode;
            TreeNode nodes;
            TreeNode node;
            
    //循环获取节点中属性ID的值
            foreach (XmlNode xn in nodeList)//遍历所有子节点
            {
                XmlElement xe 
    = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                for (int m = 0; m < 3; m++)
                
    {

                    
    if (xe.GetAttribute("ID"== m.ToString())//如果ID属性值为m的值
                    {
                        treenode 
    = new TreeNode(xe.GetAttribute("Text").ToString(),"","",xe.GetAttribute("URL").ToString(),"");

                        
    this.TreeView1.Nodes.Add(treenode); //为树添加1级节点

                        
    //Response.Write(xe.GetAttribute("Text").ToString() + "</br>");

                        
    //如果下面有子节点在下走
                        XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                        foreach (XmlNode xn1 in nls)//遍历
                        {
                            XmlElement xe2 
    = (XmlElement)xn1;//转换类型
                            if (xe2.Name == "Nodes")//如果找到
                            {
                                
    for (int i = 0; i < nls.Count; i++)
                                
    {
                                    
    //为2级节点添加子元素
                                    nodes = new TreeNode(nls.Item(i).Attributes[0].Value, """", nls.Item(i).Attributes[1].Value, "");
                                    treenode.ChildNodes.Add(nodes);
                                    
    //this.TreeView1.Nodes.Add(nodes);
                                    
    //Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + nls.Item(i).Attributes[0].Value + "</br>");
                                    
    //如果下面有子节点再下走
                                    XmlNodeList nls1 = xe2.ChildNodes;//继续获取xe2子节点的所有子节点
                                    foreach (XmlNode xn2 in nls1)//遍历
                                    {
                                        XmlElement xe3 
    = (XmlElement)xn2;//转换类型
                                        if (xe3.Name == "Node")//如果找到
                                        {
                                            
    for (int j = 0; j < nls1.Count; j++)
                                            
    {
                                                
    //为3级节点添加子节点
                                                node = new TreeNode(nls1.Item(j).Attributes[0].Value, """", nls.Item(i).Attributes[1].Value, "");
                                                nodes.ChildNodes.Add(node);
                                                
    //Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + nls1.Item(j).Attributes[0].Value + "</br>");
                                            }

                                            
    break;
                                        }

                                    }

                                }

                                
    break;
                            }

                        }

                        
    break;
                    }

                }

            }

        }

  • 相关阅读:
    FortiGate 硬件加速
    RSA modulus too small: 512 < minimum 768 bits
    VMXNET3 vs E1000E and E1000
    BZOJ 1432: [ZJOI2009]Function(新生必做的水题)
    BZOJ 2456: mode(新生必做的水题)
    BZOJ 1968: [Ahoi2005]COMMON 约数研究(新生必做的水题)
    BZOJ 2463: [中山市选2009]谁能赢呢?(新生必做的水题)
    海量数据处理算法总结【超详解】
    POJ 1659 Frogs' Neighborhood(可图性判定—Havel-Hakimi定理)【超详解】
    图的存储结构之邻接表(详解)
  • 原文地址:https://www.cnblogs.com/sunfeiwto/p/1334864.html
Copyright © 2020-2023  润新知