Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML formatting

Status
Not open for further replies.

Kenny62

Programmer
Mar 3, 2004
54
GB
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>
 

might try reading your output into a
dataset and let its write method take care
of your formatting for you.


Dim ds As New DataSet
ds.ReadXml(Filename)
ds.WriteXml("outfile.xml")

if it is to be it's up to me
 
This is inefficient in practice - as we are now doing two write operations and one read. All i wanted to do was write a properly indented XML file. (Anyway thanks for the suggestion nfinitelo - i'll give it a try).
 
There is no such thing as a properly indented XML file.
Anything outside a node is counted as whitespace, which according to the W3C spec gets ignored when read back in.

An XML reader (DOM or SAX) will be perfectly happy with it all on one line. Any additional whitespace just bulks up the file unnecessarily.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top