通过xml实现根据权限建立目录树
我刚刚学习.net和xml,写的不是很规范,只是能实现我想要的了
我也不知道此方法是好是坏,在这里抛砖引玉,希望大家多提意见。
这个方法有一些局限性,不是那种无限添加的目录树,兄弟树只有9个,我想大家看我的xml就能明白了。
下面是我的xml(f.xml):
<?xml version="1.0" encoding="gb2312"?>
<tree>
<name id="0">a</name>
<contents>
<name id="1" r="1">b1</name>
<contents>
<name id="11" r="2">c1</name>
</contents>
<name id="2" r="3">b2</name>
<contents>
<name id="21" r="4">c2</name>
<name id="22" r="5">c3</name>
<name id="23" r="6">c4</name>
</contents>
<name id="3" r="7">b3</name>
<contents>
<name id="31" r="8">c5</name>
</contents>
</contents>
</tree>
下面是我的建立目录树的代码,我没有采用递归的方法
using System.Xml;
using System.Text.RegularExpressions;
protected void Page_Load(object sender, EventArgs e)
{
string func = "1,2,5,4,3,"; //这个是代表权限的字符串
XmlTextReader xtr = new XmlTextReader(Server.MapPath("~/f.xml"));
while (xtr.Read())
{
if (xtr.GetAttribute("id") == "0")
{
string s = xtr.ReadInnerXml();
if (s == "a")
{
TreeNode nt = new TreeNode(s);
TreeView1.Nodes.Add(nt);
}
}
for (int i = 1; i <= 9; i++)
{
if (xtr.GetAttribute("id") == i.ToString())
{
if (Regex.IsMatch(func, "\\b" + xtr.GetAttribute("r") + "\\b"))
{
string s = xtr.ReadInnerXml();
TreeNode tn = new TreeNode(s);
TreeView1.Nodes[0].ChildNodes.Add(tn);
}
}
}
for (int ii = 11; ii <= 99; ii++)
{
if (xtr.GetAttribute("id") == ii.ToString())
{
if (Regex.IsMatch(func, "\\b" + xtr.GetAttribute("r") + "\\b"))
{
string s = xtr.ReadInnerXml();
if (TreeView1.Nodes[0].ChildNodes.Count > 0)
{
int i = TreeView1.Nodes[0].ChildNodes.Count - 1;
TreeNode tn = new TreeNode(s);
TreeView1.Nodes[0].ChildNodes[i].ChildNodes.Add(tn);
}
}
}
}
}
}
这里有个地方需要注意,就是权限字符串,一定要保证子树如果有权限,那么他的父目录树一定也要有权限,要不然就会有错误,呵
呵
页面的代码:
<form id="form1" runat="server">
<asp:TreeView ID="TreeView1" runat="server" Height="264px" Width="167px" ExpandDepth="0" />
</form>
希望大家批评指正
--------------------编程问答-------------------- --------------------编程问答-------------------- 非常一般,不过喜欢你的精神. --------------------编程问答-------------------- 楼上的兄弟,帮忙看看有没有更好的办法,或者帮我改进下xml
补充:.NET技术 , C#