By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,810 Members | 783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,810 IT Pros & Developers. It's quick & easy.

Save contents of treeview to XML

P: n/a
Hi all,

I have some problem while saving treeview's content to xml file. I have no experience dealing with xml file before. I did some leaning and searching on the web and found some sample codes in vb 6.0(i'm writing in vb.net). The code fails when tried to create a new XMLElement that consist of special characters like "&", "$", " "(space),etc.
Here's my code, perhaps anyone could advice me how solve this problem, or perhaps there's a better way to have this done.

Thanks

Yanto

Code:
Dim xmlDoc As New XmlDocument
Dim elementNode As XmlElement
Dim newElementNode As XmlElement
Dim rootElementNode As XmlElement

elementNode = xmlDoc.CreateElement("MyNode")
rootElementNode = xmlDoc.AppendChild(elementNode)

Dim iLp As Integer

For iLp = 0 To tvwCube.Nodes.Count - 1

elementNode = xmlDoc.CreateElement(tvwCube.Nodes(iLp).Text)
elementNode.SetAttribute("Caption", tvwCube.Nodes(iLp).Text)

newElementNode = rootElementNode.AppendChild(elementNode)

LoopChildren(tvwCube.Nodes(iLp), newElementNode)
Next iLp

xmlDoc.Save("C:\CubeMetadata.xml")
Private Sub LoopChildren(ByVal node As TreeNode, ByVal xNode As XmlNode)

Dim newChildElement As XmlElement
Dim newElementNode As XmlElement

Dim child As TreeNode
Dim strNode As String

For Each child In node.Nodes
'##### the code fails here, when there's a char like "$", "%","&" in strNode
newChildElement = xNode.OwnerDocument.CreateElement(strNode)
newChildElement.SetAttribute("Caption", child.Text)

newElementNode = xNode.AppendChild(newChildElement)

If child.GetNodeCount(False) > 0 Then
LoopChildren(child, newElementNode)
End If
Next child

End Sub
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.