Treeview to XML

Sep 1 2009 2:24 AM

I want to convert treeview back to an xml.  Here I attached my coding for treeview. I need to get an xml from this treeview. Itz Very urgent. Please help me out....
Sub DisplayXmlFile(ByVal filename As String, ByVal tvw As TreeView)
Dim xmldoc As New XmlDocument
xmldoc.Load(filename)
' Add it to the TreeView Nodes collection
DisplayXmlNode(xmldoc, tvw.Nodes)
' Expand the root node.
tvw.Nodes(0).Expand()
End Sub
Sub DisplayXmlNode(ByVal xmlnode As XmlNode, ByVal nodes As TreeNodeCollection)
' Add a TreeView node for this XmlNode.
' (Using the node's Name is OK for most XmlNode types.)
Dim tvNode As TreeNode
If xmlnode.Name = "#comment" Or xmlnode.Name = "#document" Or xmlnode.Name = "xml" Then 'skipping comment node
Else
tvNode = nodes.Add(xmlnode.Name)
tvNode.ForeColor = Color.Navy
End If
Select Case xmlnode.NodeType
Case XmlNodeType.Element
' This is an element: Check whether there are attributes.
If xmlnode.Attributes.Count > 0 Then
' Create an ATTRIBUTES node.
tvNode.ForeColor = Color.Purple
Dim attrNode As TreeNode = tvNode.Nodes.Add("ATTRIBUTES")
' Add all the attributes as children of the new node.
Dim xmlAttr As XmlAttribute
For Each xmlAttr In xmlnode.Attributes
attrNode.ForeColor = Color.Green
' Each node shows name and value.
attrNode.Nodes.Add(xmlAttr.Name &
" = '" & xmlAttr.Value & _
"'")

Next
End If
Case XmlNodeType.Text, XmlNodeType.CDATA
' For these node types we display the value
tvNode.Text = xmlnode.Value
Case XmlNodeType.Comment
'tvNode.ForeColor = Color.White
'tvNode.Text = "<!--" & xmlnode.Value & "-->"
Case XmlNodeType.ProcessingInstruction, XmlNodeType.XmlDeclaration
tvNode.ForeColor = Color.Red
tvNode.Text = "<?" & xmlnode.Name & " " & xmlnode.Value & "?>"
Case Else
' ignore other node types.
End Select
' Call this routine recursively for each child node.
Dim xmlChild As XmlNode = xmlnode.FirstChild
Do Until xmlChild Is Nothing
If tvNode Is Nothing Then
DisplayXmlNode(xmlChild, nodes)
xmlChild = xmlChild.NextSibling
Else
DisplayXmlNode(xmlChild, tvNode.Nodes)
xmlChild = xmlChild.NextSibling
End If
Loop
End Sub

Answers (1)