treeview如何使用XML文件

使用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

没有进行更深的嵌套测试,如果大家有兴趣可以自己修改实验一下!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霜吹花落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值