Hi - I've written a recursive routine to save the contents
of a treeview to XML file. This works - but only the root
node is correctly formatted. All other nodes are on the
same line: Any idea whay this happens and how can i fix it.
Thanks in advance for any help.
Code sample:
Private Sub exportToXml(ByRef tv As TreeView, ByVal Filename As String)
Dim ThisNode As TreeNode
xr = New XmlTextWriter(Filename, System.Text.Encoding.UTF8)
xr.Formatting = Formatting.Indented
xr.Indentation = 4
xr.QuoteChar = """"c
xr.WriteStartDocument()
'Write our root node
xr.WriteStartElement(tvOptions.Nodes(0).Text)
For Each ThisNode In tv.Nodes
'For some unkonwon reason XML tag - besides the root are not
'indented, why? Fix this - later...
SaveNode(ThisNode.Nodes)
Next
'Close the root node
xr.WriteEndElement()
xr.WriteEndDocument()
xr.Close()
End Sub
Private Sub SaveNode(ByVal tnc As TreeNodeCollection)
Dim ThisNode As TreeNode
For Each ThisNode In tnc
'If we have child nodes, we'll write a parent node, then iter-rate through
'the children...
If (ThisNode.Nodes.Count > 0) Then
xr.WriteStartElement(ThisNode.Text)
SaveNode(ThisNode.Nodes)
xr.WriteEndElement()
Else 'No child nodes, so we just write the text
xr.WriteString(ThisNode.Text)
End If
Next
End Sub
XML output example:
<family>
<parent>id="grandxxxfather"<parent>id="father"<parent>id="brother"<child>id="niece"</child></parent><parent>id="me"<child>id="son"</child><child>id="daughter"</child></parent><child>id="sister"</child></parent><parent>id="uncle"<parent>id="cousin sister"<child>id="second cousin"</child></parent><child>id="cousin brother"</child></parent></parent>
</family>
of a treeview to XML file. This works - but only the root
node is correctly formatted. All other nodes are on the
same line: Any idea whay this happens and how can i fix it.
Thanks in advance for any help.
Code sample:
Private Sub exportToXml(ByRef tv As TreeView, ByVal Filename As String)
Dim ThisNode As TreeNode
xr = New XmlTextWriter(Filename, System.Text.Encoding.UTF8)
xr.Formatting = Formatting.Indented
xr.Indentation = 4
xr.QuoteChar = """"c
xr.WriteStartDocument()
'Write our root node
xr.WriteStartElement(tvOptions.Nodes(0).Text)
For Each ThisNode In tv.Nodes
'For some unkonwon reason XML tag - besides the root are not
'indented, why? Fix this - later...
SaveNode(ThisNode.Nodes)
Next
'Close the root node
xr.WriteEndElement()
xr.WriteEndDocument()
xr.Close()
End Sub
Private Sub SaveNode(ByVal tnc As TreeNodeCollection)
Dim ThisNode As TreeNode
For Each ThisNode In tnc
'If we have child nodes, we'll write a parent node, then iter-rate through
'the children...
If (ThisNode.Nodes.Count > 0) Then
xr.WriteStartElement(ThisNode.Text)
SaveNode(ThisNode.Nodes)
xr.WriteEndElement()
Else 'No child nodes, so we just write the text
xr.WriteString(ThisNode.Text)
End If
Next
End Sub
XML output example:
<family>
<parent>id="grandxxxfather"<parent>id="father"<parent>id="brother"<child>id="niece"</child></parent><parent>id="me"<child>id="son"</child><child>id="daughter"</child></parent><child>id="sister"</child></parent><parent>id="uncle"<parent>id="cousin sister"<child>id="second cousin"</child></parent><child>id="cousin brother"</child></parent></parent>
</family>