Friend Module Module1
Public Sub kcXML2Tree(ByVa l 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 ArgumentNullExc eption("TreeVie w")
End If
Try
XMLDoc.Load(XML Filepath)
Catch e As Exception
Throw New Exception(e.Mes sage)
End Try
For Each XMLNode In XMLDoc.Document Element.ChildNo des
ParentNode = kcTreeview.Node s.Add(XMLNode.A ttributes(0).Va lue)
If XMLNode.ChildNo des.Count > 0 Then
AddChildNodes(X MLNode, ParentNode)
End If
Next
End Sub
Private Sub AddChildNodes(B yVal XMLNode As Xml.XmlNode, ByVal ParentNode
As TreeNode)
Dim ChildXMLNode As Xml.XmlNode
Dim NewNode As TreeNode
For Each ChildXMLNode In XMLNode.ChildNo des
NewNode = ParentNode.Node s.Add(ChildXMLN ode.Attributes( 0).Value)
If ChildXMLNode.Ch ildNodes.Count > 0 Then
AddChildNodes(C hildXMLNode, NewNode)
End If
Next
End Sub
Public Sub kcTree2XML(ByVa l 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.XmlAttribut e
XMLDoc.LoadXml( ("<?xml version='1.0' ?>" & _
"<XMLTreeVi ew>" & _
"</XMLTreeView>"))
For Each RootNode In kcTreeview.Node s
NewXMLNode = XMLDoc.CreateNo de(Xml.XmlNodeT ype.Element, "Node",
"Node", "")
XMLAttribute = XMLDoc.CreateAt tribute("name")
XMLAttribute.Va lue = RootNode.Text
NewXMLNode.Attr ibutes.Append(X MLAttribute)
XMLDoc.Document Element.AppendC hild(NewXMLNode )
SaveChildNodes( NewXMLNode, RootNode, XMLDoc)
Next
XMLDoc.Save(XML FilePath)
End Sub
Private 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.XmlAttribut e
For Each ChildNode In ParentNode.Node s
NewXMLNode = XMLDoc.CreateNo de(Xml.XmlNodeT ype.Element, "Node",
"Node", "")
XMLAttribute = XMLDoc.CreateAt tribute("name")
XMLAttribute.Va lue = ChildNode.Text
NewXMLNode.Attr ibutes.Append(X MLAttribute)
XMLNode.AppendC hild(NewXMLNode )
SaveChildNodes( NewXMLNode, ChildNode, XMLDoc)
Next
End Sub
End Module
"Supra" <su***@domain.i nvalid> wrote in message
news:JL******** ************@ro gers.com...
look on right side of website:
http://www.vbdotnetheaven.com/
C. A. Kelly wrote:
I have seen lots of posts for populating a treeview from a XML file but
haven't been able to find/figure out how to go from a treeview to XML.