zoukankan      html  css  js  c++  java
  • asp.net Treeview节点保存成XML文件

    递归treeview的节点,并保存成XML文件,让treeview无限级分类读取节点速度比较快

    //生成XML文件 

    using System.Xml;

    protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                Init_Tree();
                save();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('前台产品分类更新成功!')", true);
                Button1.Enabled = false;
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('前台产品分类更新失败!')", true);
            }
         
        }

    XmlDocument doc = new XmlDocument();
        //遍历XML根节点
        private void save()
        {
           // XmlTextWriter objXml = new XmlTextWriter(Server.MapPath("../XMLTest.xml"), null);
            doc.LoadXml("<XMLTest></XMLTest>");
            XmlNode root = doc.SelectSingleNode("XMLTest");
            foreach (TreeNode tn in TreeView_Tree.Nodes)
            {
                XmlElement Test = doc.CreateElement("treenode");
                XmlAttribute cid = doc.CreateAttribute("cid");
                cid.InnerText = tn.Value;
                Test.SetAttributeNode(cid);
                XmlAttribute treenodename = doc.CreateAttribute("name");
                treenodename.InnerText = tn.Text;
                Test.SetAttributeNode(treenodename);
                XmlAttribute treenodeUrl = doc.CreateAttribute("url");
                treenodeUrl.InnerText = tn.NavigateUrl;
                Test.SetAttributeNode(treenodeUrl);
                root.AppendChild(Test);
                if (tn.ChildNodes != null)
                {
                    ChildNods(tn, Test);
                }
            }
           doc.Save(Server.MapPath("../XMLTest.xml"));
        }
        //遍历XML子节点
        private void ChildNods(TreeNode ParentNode, XmlElement Test)
        {
            foreach (TreeNode tn in ParentNode.ChildNodes)
            {
                XmlElement Ts = doc.CreateElement("treenode");
                XmlAttribute cid = doc.CreateAttribute("cid");
                cid.InnerText = tn.Value;
                Ts.SetAttributeNode(cid);
                XmlAttribute treenode2name = doc.CreateAttribute("name");
                treenode2name.InnerText = tn.Text;
                Ts.SetAttributeNode(treenode2name);
                XmlAttribute treenode2Url = doc.CreateAttribute("url");
                treenode2Url.InnerText = tn.NavigateUrl;
                Ts.SetAttributeNode(treenode2Url);
                Test.AppendChild(Ts);
                if (tn.ChildNodes != null)
                {
                    ChildNods(tn, Ts);
                }
            }
        }

        protected void Init_Tree()
        {
            //获取Faq树形目录节点
            string sqlstr = "select * from Content_Class order by ClassOrder asc,cid desc";
            DataSet DataSet_Tree = access_sql.GreatDs(sqlstr);
            this.ViewState["DataSet_Tree"] = DataSet_Tree;
            //调用递归函数,完成树形结构的生成
            AddTree("0", (TreeNode)null);
        }
        //递归添加树的节点 参数ParentID为树的最大父结点
        public void AddTree(string ParentID, TreeNode pNode)
        {

            DataSet DataSet_Tree = (DataSet)this.ViewState["DataSet_Tree"];
            DataView DataView_Tree = new DataView(DataSet_Tree.Tables[0]);
            //过滤ParentID,得到当前的所有子节点 ParentID为父节点ID
            DataView_Tree.RowFilter = " ClassPre= '" + ParentID + "' ";
            DataView_Tree.Sort = " ClassOrder ASC ";
            //循环递归

            foreach (DataRowView Row in DataView_Tree)
            {

                //声明节点
                TreeNode Node = new TreeNode();
                //绑定超级链接
                // Node.NavigateUrl = baseinfo.Getpclassurl(Row["classname"].ToString(), Row["cid"].ToString());
                string classname = access_sql.GetOneValue("select ClassName from Content_Class where cID=" + Row["cid"].ToString());
                Node.NavigateUrl = baseinfo.Getpclassurl(classname, Row["cid"].ToString());

     


                bool flag = false;

                //开始递归      
                if (pNode == null)
                {
                    //添加根节点
                    Node.Text = Row["classname"].ToString();
                    Node.Value = Row["cid"].ToString();
                    TreeView_Tree.Nodes.Add(Node);
                    Node.Expanded = flag; //节点状态闭
                    AddTree(Row["ClassId"].ToString(), Node);    //再次递归
                }
                else
                {
                    //添加当前节点的子节点
                    Node.Text = Row["classname"].ToString();
                    Node.Value = Row["cid"].ToString();
                    pNode.ChildNodes.Add(Node);
                    Node.Expanded = flag; //节点状态闭
                    AddTree(Row["ClassId"].ToString(), Node);     //再次递归
                }
            }


        }

  • 相关阅读:
    深入解读kubernetes网络基本原理
    Go!Go!Go!设计模式-组合设计模式
    Go!Go!Go!设计模式-创建型模式(简单工厂,工厂方法,抽象工厂)
    Linux内核之磁盘和分区
    Docker容器网络基础
    chart仓库之创建-入库-使用(helm,helm-push,chartmuseum)
    Go语言完整解析Go!Go!Go!(一)数据类型 之 Channel & Goroutine
    k8s爬坑集锦[网络问题]-服务无法访问
    数字证书的原理与应用&爬坑
    ingress的用法与原理
  • 原文地址:https://www.cnblogs.com/wyxy2005/p/1370822.html
Copyright © 2011-2022 走看看