自定义 XML 文件在 TreeView 控件中的使用

来源:互联网 发布:js 判断不等于空 编辑:程序博客网 时间:2024/06/08 01:02
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

在 Asp.Net 中,可以很方便地使用由微软提供的 Internet Exploer Web Controls 控件来实现树形列表。由微软提供的这套控件集合中包括有 MultiPage,TabStrip,Toolbar,TreeView 四个控件。

关于这几个控件的使用在微软的网站由详细的说明(参见:TreeViewiewebcontrol.asp">http://www.microsoft.com/china/msdn/archives/library/dnaspp/html/aspnet-usingTreeViewiewebcontrol.asp; http://msdn.microsoft.com/library/default.asp?url=/workshop/webcontrols/overview/overview.asp)。

在微软提供的文档中 TreeView 控件支持 XML 文件作为资源文件XML 文件的结构本来就是一种树型结构),关于在 TreeView 控件中如可使用 XML 文件,很多资料有详细的介绍(http://www.yesky.com/SoftChannel/72342380468043776/20040630/1825811.shtml),本文介绍的则是用另外一种方式解析 XML 文件TreeView 控件中

因为该方法是我在没有找到资料的情况下为了方便使用该控件而自行写的一个处理过程,其实在使用的时候已经大不可必,写出来,只是出于共同学习的目的。

在拿到这个控件的时候,我对其使用方法是不大了解的,但为了让自己在以后的程序中能够较为方便的使用之,所以我决定采用使用 XML 文件的方式以存储节点信息,所以首先我构建了一个 XML 文件基本格式:

XML version="1.0" encoding="UTF-8"?><?XML version="1.0" encoding="UTF-8"?><TreeView> <nodes>  <text>根节点</text>  <link></link>  <target>_self</target>  <description></description>  <node>   <text>测试节点 -1</text>   <link>/test1.aspx</link>   <target>MainFrame</target>   <description></description>  </node>  <node>   <text>测试节点 - 2</text>   <link>/test2.aspx</link>   <target>_top</target>   <description></description>  </node> </nodes></TreeView>

TreeView>TreeView 为根结点,nodes 为父级节点,node 为子节点。其中 nodes 可以嵌套,可以并行,node 只能并行。在定义好结构后,最主要的就是如何解析了。下面是具体的代码:

  ///

  /// TreeViewParse TreeView解析器,从XML文件中读取节点值  /// document XML文档  /// TreeView Microsoft.Web.UI.WebControls.TreeView  ///   private void TreeViewParse(System.XML.XMLNode document, Microsoft.Web.UI.WebControls.TreeView TreeView)  {   if(document.Name != "TreeView") return;   foreach(System.XML.XMLNode node in document.ChildNodes)   {    if(node.Name != "nodes") return;    this.NodesParse(node, TreeView, null);   }  }

  ///

  /// NodesParse Nodes解析器,从XML文件中读取主节点值  /// document XML文档  /// TreeView Microsoft.Web.UI.WebControls.TreeView  /// treeNode 父级节点  ///   private void NodesParse(System.XML.XMLNode document, Microsoft.Web.UI.WebControls.TreeView TreeView, Microsoft.Web.UI.WebControls.TreeNode treeNode)  {   if(document.Name != "nodes") return;   Microsoft.Web.UI.WebControls.TreeNode child = new Microsoft.Web.UI.WebControls.TreeNode();   foreach(System.XML.XMLNode node in document.ChildNodes)   {    string name = (node.Name != null ? node.Name : "");    // child.ID = node.Name + "_" + TreeView.Nodes.Count.ToString();    switch(name.Trim().ToLower())    {     case "text":      child.Text = node.InnerText;      break;     case "link":      child.NavigateUrl = node.InnerText;      break;     case "target":      child.Target = node.InnerText;      break;     case "nodes":      NodesParse(node, TreeView, child);      break;     case "node":      NodeParse(node, TreeView, child);      break;    }   }   if(treeNode == null) TreeView.Nodes.Add(child);   else treeNode.Nodes.Add(child);  }

  ///

  /// NodeParse Node解析器,从XML文件中读取子节点值  /// document XML文档  /// TreeView Microsoft.Web.UI.WebControls.TreeView  /// treeNode 父级节点  ///   private void NodeParse(System.XML.XMLNode document, Microsoft.Web.UI.WebControls.TreeView TreeView, Microsoft.Web.UI.WebControls.TreeNode treeNode)  {   Microsoft.Web.UI.WebControls.TreeNode child = new Microsoft.Web.UI.WebControls.TreeNode();   foreach(System.XML.XMLNode node in document.ChildNodes)   {    string name = (node.Name != null ? node.Name : "");    switch(name.Trim().ToLower())    {     case "text":      child.Text = node.InnerText;      break;     case "link":      child.NavigateUrl = node.InnerText;      break;     case "target":      child.Target = node.InnerText;      break;     case "nodes":      NodesParse(node, TreeView, child);      break;    }   }   treeNode.Nodes.Add(child);  }

以下是使用方法:

  private Microsoft.Web.UI.WebControls.TreeView MSTreeView = new Microsoft.Web.UI.WebControls.TreeView();   System.XML.XMLDocument document = new System.XML.XMLDocument(); //   document.Load(System.Web.HttpContext.Current.Server.MapPath(this.XMLDocument));   MSTreeView.Nodes.Clear();   TreeViewParse(document.DocumentElement, MSTreeView);

到此,基本结束,此时就可以在页面正确显示自行定义的 XML 文档的树目录,但我还不认为方便,于是将其综合后写成了一个组件,于是在使用的时候只需拖放到页面上……但后来在找资料的时候找到了其实 TreeView  控件本身就可以直接使用 XML 文档(http://www.yesky.com/SoftChannel/72342380468043776/20040630/1825811.shtml)。所以,这个方法就只能是一段用以学习的代码了!

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>