asp.net TreeView与XML三步生成列表树
刚我在做tree view 绑定时自己摸索了一下,网上有人说treeview绑定数据源,用什么递归绑定啥的,我不想看了,就自己试着写了一个
我是这样做的,如果有什么问题请大神指导,我是菜鸟额..
1:页面treeview 叫tvfunction
2:新建 adminfunction.xml
3:页面绑定
/// <summary>
/// 初始化功能树结构
/// </summary>
private void initfunction()
{
xmldocument xmldoc = new xmldocument();
string xmlpath = "~/adminfunction.xml";
xmlpath = server.mappath(xmlpath);
xmldoc.load(xmlpath);
xmlnode root = xmldoc.documentelement;
foreach (xmlnode node in root.childnodes)
{
//获取父节点的信息显示(轻狂-书生)
xmlelement x1 = node as xmlelement;
string name = x1.getattribute("name");
string url = x1.getattribute("url");
treenode tn = new treenode();
tn.text = name;
tn.navigateurl = url;
foreach (xmlnode subnode in x1.childnodes)
{
xmlelement subxmlemt = subnode as xmlelement;
string subname = subxmlemt.getattribute("name");
string suburl = subxmlemt.getattribute("url");
treenode subtn = new treenode();
subtn.text = subname;
subtn.navigateurl = suburl;
tn.childnodes.add(subtn);
}
tvfunction.nodes.add(tn);
}
}
4:显示效果