ToolCollection/ToolCollection/modSaveTreeView.vb
Jonathan Jenne 688f53a74a jj 31.05
2017-05-31 12:17:35 +02:00

86 lines
2.7 KiB
VB.net

Module modSaveTreeView
Public Sub XML2TreeView(ByVal XMLFilepath As String, ByVal kcTreeview As TreeView)
Dim XMLDoc As New Xml.XmlDocument
Dim XMLNode As Xml.XmlNode
Dim ParentNode As TreeNode
If kcTreeview Is Nothing Then
Throw New ArgumentNullException("TreeView")
End If
Try
XMLDoc.Load(XMLFilepath)
Catch e As Exception
Throw New Exception(e.Message)
End Try
For Each XMLNode In XMLDoc.DocumentElement.ChildNodes
ParentNode = kcTreeview.Nodes.Add(XMLNode.Attributes(0).Value)
If XMLNode.ChildNodes.Count > 0 Then
AddChildNodes(XMLNode, ParentNode)
End If
Next
End Sub
Public Sub AddChildNodes(ByVal XMLNode As Xml.XmlNode, ByVal ParentNode As TreeNode)
Dim ChildXMLNode As Xml.XmlNode
Dim NewNode As TreeNode
For Each ChildXMLNode In XMLNode.ChildNodes
NewNode = ParentNode.Nodes.Add(ChildXMLNode.Attributes(0).Value)
If ChildXMLNode.ChildNodes.Count > 0 Then
AddChildNodes(ChildXMLNode, NewNode)
End If
Next
End Sub
Public Sub treeView2XML(ByVal XMLFilePath As String, ByVal kcTreeview As TreeView)
Dim XMLDoc As New Xml.XmlDocument
Dim RootNode As TreeNode
Dim NewXMLNode As Xml.XmlNode
Dim XMLAttribute As Xml.XmlAttribute
XMLDoc.LoadXml(("<?xml version='1.0' ?>" & _
"<XMLTreeView>" & _
"</XMLTreeView>"))
For Each RootNode In kcTreeview.Nodes
NewXMLNode = XMLDoc.CreateNode(Xml.XmlNodeType.Element, "Node",
"Node", "")
XMLAttribute = XMLDoc.CreateAttribute("name")
XMLAttribute.Value = RootNode.Text
NewXMLNode.Attributes.Append(XMLAttribute)
XMLDoc.DocumentElement.AppendChild(NewXMLNode)
SaveChildNodes(NewXMLNode, RootNode, XMLDoc)
Next
XMLDoc.Save(XMLFilePath)
End Sub
Public Sub SaveChildNodes(ByVal XMLNode As Xml.XmlNode, ByVal ParentNode As TreeNode, ByVal XMLDoc As Xml.XmlDocument)
Dim ChildNode As TreeNode
Dim NewXMLNode As Xml.XmlNode
Dim XMLAttribute As Xml.XmlAttribute
For Each ChildNode In ParentNode.Nodes
NewXMLNode = XMLDoc.CreateNode(Xml.XmlNodeType.Element, "Node",
"Node", "")
XMLAttribute = XMLDoc.CreateAttribute("name")
XMLAttribute.Value = ChildNode.Text
NewXMLNode.Attributes.Append(XMLAttribute)
XMLNode.AppendChild(NewXMLNode)
SaveChildNodes(NewXMLNode, ChildNode, XMLDoc)
Next
End Sub
End Module