Save and Load Treeviews in

Imports System.Xml

Public Class TreeView
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub populateTreeview()

        'Dim xDoc As XmlDocument = New XmlDocument()
        'TreeView1.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name))
        'Dim tNode As TreeNode = New TreeNode()
        'tNode = CType(TreeView1.Nodes(0), TreeNode)
        'addTreeNode(xDoc.DocumentElement, tNode)
    End Sub

    Private Sub addTreeNode(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode)
        Dim xNode As XmlNode
        Dim tNode As TreeNode
        Dim xNodeList As XmlNodeList

        If xmlNode.HasChildNodes Then
            xNodeList = xmlNode.ChildNodes

            For x As Integer = 0 To xNodeList.Count - 1
                xNode = xmlNode.ChildNodes(x)
                treeNode.ChildNodes.Add(New TreeNode(xNode.Name))
                tNode = treeNode.ChildNodes(x)
                addTreeNode(xNode, tNode)
            treeNode.Text = xmlNode.OuterXml.Trim()
        End If
    End Sub

    Private xr As XmlTextWriter

    Public Sub exportToXml2(ByVal tv As TreeView, ByVal filename As String)
        xr = New XmlTextWriter(filename, System.Text.Encoding.UTF8)

        For Each node As TreeNode In tv.Items

    End Sub

    Private Sub saveNode2(ByVal tnc As TreeNodeCollection)
        For Each node As TreeNode In tnc

            If node.ChildNodes.Count > 0 Then
            End If
    End Sub

    Public Shared PublicXML As String = Nothing
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MyTreeNode As TreeNode
        Dim MyTreeNodeChild As TreeNode

        For i = 0 To 50
            MyTreeNode = New TreeNode
            MyTreeNode.Text = "test" & i.ToString

            Dim RandomNumber As Integer = myRandom.Next(0, 2)
            If RandomNumber = 1 Then
                For ii = 0 To myRandom.Next(0, 10)
                    MyTreeNodeChild = New TreeNode
                    MyTreeNodeChild.Text = "testchild" & ii.ToString
                    MyTreeNodeChild = Nothing
            End If

            MyTreeNode = Nothing


        PublicXML = PublicModule.XmlSerialize(TreeView1.Nodes)

    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim MyNodes As TreeNodeCollection = PublicModule.XmlDeserialize(Of TreeNodeCollection)(PublicXML)

        For Each MyNode In MyNodes
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    End Sub
End Class
Imports System.IO
Imports System.Web
Imports System.Xml.Serialization

Public Module PublicModule
    Public myRandom As New Random()

#Region "Data"
    Public Function XmlDeserialize(Of T)(ByVal toDeserialize As String) As T
        Dim xmlSerializer As XmlSerializer = New XmlSerializer(GetType(T))

        Using textReader As StringReader = New StringReader(toDeserialize)
            Return CType(xmlSerializer.Deserialize(textReader), T)
        End Using
    End Function

    Public Function XmlSerialize(Of T)(ByVal toSerialize As T) As String
        Dim xmlSerializer As XmlSerializer = New XmlSerializer(GetType(T))

        Using textWriter As StringWriter = New StringWriter()
            xmlSerializer.Serialize(textWriter, toSerialize)
            Return textWriter.ToString()
        End Using
    End Function
#End Region
End Module
Public Class MyModule
    Implements IHttpModule

    Private WithEvents _context As HttpApplication

    ''' <summary>
    '''  You will need to configure this module in the Web.config file of your
    '''  web and register it with IIS before being able to use it. For more information
    '''  see the following link:
    ''' </summary>
#Region "IHttpModule Members"

    Public Sub Dispose() Implements IHttpModule.Dispose

        ' Clean-up code here

    End Sub

    Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
        _context = context
    End Sub

#End Region

    Public Sub OnLogRequest(ByVal source As Object, ByVal e As EventArgs) Handles _context.LogRequest

        ' Handles the LogRequest event to provide a custom logging 
        ' implementation for it

    End Sub

End Class

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *