Save and Load Treeviews in ASP.net

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()
        'xDoc.Load(dlg.FileName)
        'TreeView1.Nodes.Clear()
        '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)
            Next
        Else
            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)
        xr.WriteStartDocument()
        xr.WriteStartElement(TreeView1.Nodes(0).Text)

        For Each node As TreeNode In tv.Items
            saveNode2(node.ChildNodes)
        Next

        xr.WriteEndElement()
        xr.Close()
    End Sub

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

            If node.ChildNodes.Count > 0 Then
                xr.WriteStartElement(node.Text)
                saveNode2(node.ChildNodes)
                xr.WriteEndElement()
            Else
                xr.WriteString(node.Text)
            End If
        Next
    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
            TreeView1.Nodes.Add(MyTreeNode)

            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
                    MyTreeNode.ChildNodes.Add(MyTreeNodeChild)
                    MyTreeNodeChild = Nothing
                Next
            End If

            MyTreeNode = Nothing

        Next

        PublicXML = PublicModule.XmlSerialize(TreeView1.Nodes)

        Debug.WriteLine(PublicXML)
    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
            TreeView1.Nodes.Add(MyNode)
        Next
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        TreeView1.Nodes.Clear()
    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: https://go.microsoft.com/?linkid=8101007
    ''' </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 *