递归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); //再次递归
}
}
}