Save contents of treeview to XML

G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top