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
相关文章推荐
- Silverlight TreeView 动态绑定Xml 文件
- Silverlight TreeView 动态绑定Xml 文件
- Silverlight TreeView 动态绑定Xml 文件
- net9:磁盘目录文件保存到XML文档及其XML文档的读写操作,以及绑定XML到treeview
- 将XML文件绑定到TreeView,建立树形导航
- 将XML文件绑定到TreeView,建立树形导航
- treeview 绑定文件夹和文件
- 某一目录下的所有 xml文件绑定到不同的datagrid的方法
- ASP.NET实现TreeView的XML数据源绑定
- treeview绑定硬盘数据文件
- 读取XML文件动态绑定TreeView控件练习题
- xml文件读取后绑定到gridview
- c# comboBox 使用 linq to xml 绑定 XML文件
- ASP.NET实现TreeView的XML数据源绑定实例代码
- TreeView绑定XML
- 从XML文件中读取数据绑定到DropDownList-.Net编程教程
- 关于.net中控件与xml文件绑定心得
- treeview绑定xml
- winform实现 绑定xml文档到treeview 控件