您的位置:首页 > 其它

TreeView 绑定XML文件

2011-05-23 16:10 471 查看
public partial class adminindex : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("./Web.xml"));
//读取根节点
XmlElement xmlele = xmldoc.DocumentElement;
TreeNode tn = new TreeNode();
tn.Text = xmlele.ChildNodes[0].Attributes["title"].Value;
//获取根节点下的所有节点
XmlNodeList xmlnl = xmlele.ChildNodes[0].ChildNodes;
//递归遍历节点
TreeNode tn_nodes = null;
foreach (XmlNode xmlnode in xmlnl)
{
if (xmlnode.HasChildNodes)
{
tn_nodes = new TreeNode();
tn_nodes.Text = xmlnode.Attributes["title"].Value;
if (xmlnode.Attributes["url"] != null)
tn_nodes.NavigateUrl = xmlnode.Attributes["url"].Value;
GetNodes(xmlnode, tn_nodes);
tn.ChildNodes.Add(tn_nodes);
}
else
{
tn_nodes = new TreeNode();
tn_nodes.Text = xmlnode.Attributes["title"].Value;
if (xmlnode.Attributes["url"] != null)
tn_nodes.NavigateUrl = xmlnode.Attributes["url"].Value;
tn.ChildNodes.Add(tn_nodes);
}
}
TreeView1.Nodes.Add(tn);

}
}
/// <summary>
/// //递归遍历节点
/// </summary>
/// <param name="xmlnode">当前xml文件中的节点</param>
/// <param name="tn">treeview中当前节点</param>
public void GetNodes(XmlNode xmlnd, TreeNode tn)
{
//获取根节点下的所有节点
XmlNodeList xmlnl = xmlnd.ChildNodes;
TreeNode tn_nodes = null;
foreach (XmlNode xmlnode in xmlnl)
{

if (xmlnode.ChildNodes.Count > 0)
{
tn_nodes = new TreeNode();
tn_nodes.Text = xmlnode.Attributes["title"].Value;
if (xmlnode.Attributes["url"] != null)
tn_nodes.NavigateUrl = xmlnode.Attributes["url"].Value;
GetNodes(xmlnode, tn_nodes);
tn.ChildNodes.Add(tn_nodes);
}
else
{
tn_nodes = new TreeNode();
tn_nodes.Text = xmlnode.Attributes["title"].Value;
if (xmlnode.Attributes["url"] != null)
tn_nodes.NavigateUrl = xmlnode.Attributes["url"].Value;
tn.ChildNodes.Add(tn_nodes);
}
}
}
}

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode title="系统导航" description="menue">
<siteMapNode title="文档" description="document">
<siteMapNode title="联系人->" description="linkman" url="commgl.aspx?id=lx"/>
<siteMapNode title="日志->" description="daily" url="diarygl.aspx?id=rz"/>
<siteMapNode title="编程->" description="programming" url="programgl.aspx?id=bc"/>
<siteMapNode title="课件->" description="courseware" url="coursewaregl.aspx?id=kj"/>
<siteMapNode title="论文->" description="thesis" url="thesisgl.aspx?id=lw"/>
<siteMapNode title="履历->" description="resume" url="resumegl.aspx?id=ll"/>
</siteMapNode>
<siteMapNode title="书刊" description="books">
<siteMapNode title="工科类书刊->" description="wk" url="booksgl.aspx?id=gk"/>
<siteMapNode title="理科类书刊->" description="lk" url="booksgl.aspx?id=lk"/>
<siteMapNode title="文科类书刊->" description="wk" url="booksgl.aspx?id=wk"/>
</siteMapNode>
<siteMapNode title="图像" description="pg">
<siteMapNode title="个人相片->" description="self" url="graphgl.aspx?id=gr"/>
<siteMapNode title="其它相片->" description="other" url="graphgl.aspx?id=qi"/>
</siteMapNode>
<siteMapNode title="影音" description="spacevideo">
<siteMapNode title="影视->" description="screenVideo" url="spacevideogl.aspx?id=ys"/>
<siteMapNode title="教程视频->" description="edgecam" url="spacevideogl.aspx?id=jc"/>
<siteMapNode title="音乐>" description="music" url="spacevideogl.aspx?id=ms"/>
</siteMapNode>
<siteMapNode title="软件" description="sofeware">
<siteMapNode title="应用软件->" description="app" url="softwaregl.aspx?id=yy"/>
<siteMapNode title="通讯软件->" description="comm" url="softwaregl.aspx?id=tx"/>
<siteMapNode title="工具->" description="tool" url="softwaregl.aspx?id=gj"/>
<siteMapNode title="网络->" description="networks" url="softwaregl.aspx?id=wl"/>
<siteMapNode title="用户管理" description="sofeware">
<siteMapNode title="用户管理表单->" description="user" url="userlist.aspx?id=us"/>
</siteMapNode>
</siteMapNode>
</siteMapNode>
</siteMap>

转自:http://sangai.iteye.com/blog/657350
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: