1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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 |