Imports System
Imports System.Collections
Imports System.IO
Imports System.Windows.Forms
Public Class TreeViewToXML
Private Shared xmlstring As String = ""
Private Shared sr As StreamWriter
Public Sub New()
End Sub
'''
''' Exports the given TreeView to Xml and returns a string containing all tags
'''
''' TreeView - This TreeView will be parsed
'''
Public Shared Sub exportToXml(ByVal tv As TreeView, ByVal filename As String)
sr = New StreamWriter(filename, False, System.Text.Encoding.UTF8)
sr.WriteLine("")
Dim ie As IEnumerator = tv.Nodes.GetEnumerator()
'If ie.MoveNext() Then
' Dim tn As TreeNode = DirectCast(ie.Current, TreeNode)
' sr.WriteLine("<" + tn.Text + ">")
' parseNode(tn)
'End If
For index As Integer = 1 To tv.Nodes.Count
If ie.MoveNext() Then
Dim tn As TreeNode = DirectCast(ie.Current, TreeNode)
sr.WriteLine("<" + tn.Text + ">")
parseNode(tn)
End If
Next
sr.Close()
End Sub
Private Shared Sub parseNode(ByVal tn As TreeNode)
Dim ie As IEnumerator = tn.Nodes.GetEnumerator()
Dim parentnode As String = ""
parentnode = tn.Text
'For index As Integer = 1 To tn.Nodes.Count
While ie.MoveNext()
Dim ctn As TreeNode = DirectCast(ie.Current, TreeNode)
If ctn.GetNodeCount(True) = 0 Then
sr.Write(ctn.Text)
Else
sr.Write("<" + ctn.Text + ">")
End If
If ctn.GetNodeCount(True) > 0 Then
parseNode(ctn)
End If
End While
sr.Write("" + parentnode + ">")
sr.WriteLine("")
'Next
End Sub
End Class
'Namespace TreeViewExport
' For index As Integer = 1 To tv.Nodes.Count
' If ie.MoveNext() Then
'Dim tn As TreeNode = DirectCast(ie.Current, TreeNode)
' sr.WriteLine("<" + tn.Text + ">")
' parseNode(tn)
' End If
' Next
'End Namespace
'Code placed in main form
#Region "TREEVIEW TO XML"
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TreeViewToXML.exportToXml(Me.tvMainJumpMenu, Application.StartupPath + "\test.xml")
Me.RichTextBox1.LoadFile(Application.StartupPath + "\test.xml", RichTextBoxStreamType.PlainText)
End Sub
#End Region
|