使用treeview过程中可以进行简单的设置即可连接到XML文件,但是有时我们需要一些更细致的设置,这时简单的连接就无法达到理想的效果!我在网上找到很多相关的例子,不过都不是特别理想,后来自己写了如下代码进行详细说明:
XTGL_MENU.XML文件内容
<?xml version="1.0" encoding="utf-8" ?>
<!--注意:treeview 为根结点,nodes 为父级节点,node 为子节点。-->
<!--
link:链接地址
target:打开方式
description:提示信息
expanded:默认是否打开 false关闭 TRUE 打开
select:选定时的动作 EXPAND 打开
根据自己需要可以填加删除某些属性
-->
<treeview>
<nodes>
<text>根节点1</text>
<link></link>
<target>_self</target>
<description></description>
<expanded>false</expanded>
<select>Expand</select>
<node>
<text>测试节点1 -1</text>
<link>/test1.aspx</link>
<target>MainFrame</target>
<description></description>
</node>
<node>
<text>测试节点1 - 2</text>
<link>/test2.aspx</link>
<target>_top</target>
<description></description>
</node>
</nodes>
<nodes>
<text>根节点2</text>
<link></link>
<target>_self</target>
<description></description>
<node>
<text>测试节点2 -1</text>
<link>/test1.aspx</link>
<target>MainFrame</target>
<description></description>
</node>
<node>
<text>测试节点2 - 2</text>
<link>/test2.aspx</link>
<target>_top</target>
<description></description>
</node>
</nodes>
</treeview>
读取XML文件,读取父节点,读取子节点函数如下:
Protected Sub CreatTreeView(ByVal XmLFileName As String, ByVal TreeViewName As System.Web.UI.WebControls.TreeView)
Dim Document As New System.Xml.XmlDocument 'XML文件
Document.Load(System.Web.HttpContext.Current.Server.MapPath(XmLFileName))
Dim XmlBoot As System.Xml.XmlNode 'XML文件中的根节点
XmlBoot = Document.DocumentElement
If XmlBoot.Name <> "treeview" Then
Return
End If
Dim I As Integer
Dim XmlNode As System.Xml.XmlNode
For I = 0 To XmlBoot.ChildNodes.Count() - 1
XmlNode = XmlBoot.ChildNodes(I)
If XmlNode.Name <> "nodes" Then
Return
End If
Response.Write(XmlNode.Name + "<br/>")
AddNodes(XmlNode, TreeViewName)
Next
End Sub
Protected Sub AddNodes(ByVal XmlNode1 As System.Xml.XmlNode, ByVal TreeViewName As System.Web.UI.WebControls.TreeView)
If XmlNode1.Name <> "nodes" Then
Return
End If
Dim Child As New System.Web.UI.WebControls.TreeNode
Dim Node As System.Xml.XmlNode
Dim I As Integer
For I = 0 To XmlNode1.ChildNodes.Count() - 1
Node = XmlNode1.ChildNodes(I)
Response.Write(Node.Name + Str(I) + Node.InnerText + "<br/>")
Select Case Node.Name
Case "text"
Child.Text = Node.InnerText
Case "link"
Child.NavigateUrl = Node.InnerText
Case "expanded"
If Node.InnerText = "false" Then
Child.Expanded = False
Else
Child.Expanded = True
End If
Case "select"
If Node.InnerText = "Expand" Then
Child.SelectAction = TreeNodeSelectAction.Expand
End If
Case "node"
AddNode(Node, TreeViewName, Child)
End Select
Next
TreeViewName.Nodes.Add(Child)
End Sub
Protected Sub AddNode(ByVal Node As System.Xml.XmlNode, ByVal TreeViewName As System.Web.UI.WebControls.TreeView, ByVal BootNode As System.Web.UI.WebControls.TreeNode)
Dim Child1 As New System.Web.UI.WebControls.TreeNode
Dim Node1 As System.Xml.XmlNode
Dim J As Integer
For J = 0 To Node.ChildNodes.Count() - 1
Node1 = Node.ChildNodes(J)
Response.Write(Node1.Name + Str(J) + "<br/>")
Select Case Node1.Name
Case "text"
Child1.Text = Node1.InnerText
Case "link"
Child1.NavigateUrl = Node1.InnerText
End Select
Next
BootNode.ChildNodes.Add(Child1)
End Sub
没有进行更深的嵌套测试,如果大家有兴趣可以自己修改实验一下!