以XML 填充 TreeView

步驟來創建並填充 TreeView 控件與 XML

<script type="text/javascript">loadTOCNode(2, 'summary');</script>

1. 以下 XML 示例代碼粘貼到新的名爲 " Sample.xml " 文本文件。 此文件是示例 XML 數據在本示例:
<?xml version="1.0"?>
<family>
<parent>id="grandfather"
    <parent>id="father"
         <parent>id="brother"
            <child>id="niece"
            </child>
         </parent>
         <parent>id="me"
            <child>id="son"</child>
            <child>id="daughter"</child>
         </parent>
         <child>id="sister"</child>
     </parent>
     <parent>id="uncle"
         <parent>id="cousin sister"
            <child>id="second cousin"</child>
         </parent>
         <child>id="cousin brother"</child>
     </parent>
</parent>
</family>
					
2. VisualBasic.NET 中創建新的基於 Windows 的應用程序。 默認 Form 1 被添加到應用程序。
3. 將新 TreeView 、 Button 、 Label 和 TextBox 控件拖到 Form 1。
4. 作爲在代碼窗口 Form 1 中第一行添加以下示例代碼:
Imports System.Xml
					
5. " Windows 窗體設計器生成代碼 " 節之後用以下示例代碼, 1 .vb 文件中替換整個代碼:
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      ' Initialize the controls and the form.
      Label1.Text = "File Path"
      Label1.SetBounds(8, 8, 50, 20)
      TextBox1.Text = Application.StartupPath() & "/Sample.xml"
      TextBox1.SetBounds(64, 8, 256, 20)
      Button1.Text = "Populate the TreeView with XML"
      Button1.SetBounds(8, 40, 200, 20)
      Me.Text = "TreeView control from XML"
      Me.Width = 336
      Me.Height = 368
      TreeView1.SetBounds(8, 72, 312, 264)
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Try
         ' SECTION 1. Create a DOM Document and load the XML data into it.
         Dim dom As New XmlDocument()
         dom.Load(TextBox1.Text)

         ' SECTION 2. Initialize the treeview control.
         TreeView1.Nodes.Clear()
         TreeView1.Nodes.Add(New TreeNode(dom.DocumentElement.Name))
         Dim tNode As New TreeNode()
         tNode = TreeView1.Nodes(0)

         ' SECTION 3. Populate the TreeView with the DOM nodes.
         AddNode(dom.DocumentElement, tNode)
         TreeView1.ExpandAll()

      Catch xmlEx As XmlException
         MessageBox.Show(xmlEx.Message)
      Catch ex As Exception
         MessageBox.Show(ex.Message)
      End Try

   End Sub

   Private Sub AddNode(ByRef inXmlNode As XmlNode, ByRef inTreeNode As TreeNode)
      Dim xNode As XmlNode
      Dim tNode As TreeNode
      Dim nodeList As XmlNodeList
      Dim i As Long

      ' Loop through the XML nodes until the leaf is reached.
      ' Add the nodes to the TreeView during the looping process.
      If inXmlNode.HasChildNodes() Then
         nodeList = inXmlNode.ChildNodes
         For i = 0 To nodeList.Count - 1
            xNode = inXmlNode.ChildNodes(i)
            inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
            tNode = inTreeNode.Nodes(i)
            AddNode(xNode, tNode)
         Next
      Else
         ' Here you need to pull the data from the XmlNode based on the
         ' type of node, whether attribute values are required, and so forth.
         inTreeNode.Text = (inXmlNode.OuterXml).Trim
      End If
   End Sub
					
6. 按 F 5 鍵生成並運行應用程序。 確保 XML 文件路徑是正確, 然後單擊 按鈕 。 XML 數據應顯示在 TreeView 控件。
注意 : 資源可能是 URL 或一個 XML 流文件,。 指向了有關使用 XmlDocument 類來加載 XML 數據來自不同資源本文 " 參考 " 部分。

 

前面的代碼示例映射 XML 樹數據直接到 TreeView 並顯示所有數據。 此外, 額外信息添加到顯示或跳過不需要數據。 在許多情況, 可能要顯示只屬於 XML 數據。 動態數據, 要顯示的部分可能構造, 結果是可擴展樣式表語言 (XSL) 轉換或 XPath 查詢的結果。 本節介紹如何生成新 XML 文檔只需節點, 然後將新文檔添加到 TreeView 控件。 例如, 以下步驟通過 XPath 查詢, 檢索僅子元素的 originial XML 數據, 然後將此列表作爲新節點添加到 TreeView 。

1. 粘貼以下代碼之前前面示例中 TreeView1.ExpandAll 行:
         ' SECTION 4. Create a new TreeView Node with only the child nodes.
         Dim nodelist As XmlNodeList = dom.SelectNodes("//child")
         Dim cDom As New XmlDocument()
         cDom.LoadXml("<children></children>")
         Dim node As XmlNode
         For Each node In nodelist
            Dim newElem As XmlNode = cDom.CreateNode(XmlNodeType.Element, node.Name, node.LocalName)
            newElem.InnerText = node.InnerText
            cDom.DocumentElement.AppendChild(newElem)
         Next

         TreeView1.Nodes.Add(New TreeNode(cDom.DocumentElement.Name))
         tNode = TreeView1.Nodes(1)
         AddNode(cDom.DocumentElement, tNode)
					
2. 生成並運行應用程序。 此應用程序應在 TreeView , 和原始數據顯示新 " 子代 " 根節點。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章