write treenodes to xml?

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I don't if I can add something to my question (or do I need to ask a new question, but it's a little followup).

I want to save the treeview to XML, searched and tried a lot of code I found, but it just doesn't saves the treeview as I would it have:

VB.NET:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Worshipteam>
  <Name>
    <FirstName>firstname</FirstName>
      <FullName>fullname</FullName>
      <Emailaddress>email</Emailaddress>
  </Name>
</Worshipteam>

This is the code I have to load it from the xml into the treeview:

VB.NET:
Private Sub AddressBook_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim fileI As FileInfo = New FileInfo(Application.StartupPath + "\addressbook.xml")
        If fileI.Exists Then
            Try
                Dim _XPath As String = "descendant::Name"
                Dim _rootNode As String = "Worshipteam"
                Dim _filePath As String = Application.StartupPath + "\addressbook.xml"
                Dim doc As New XmlDocument()
                doc.Load(_filePath)
                ' Load the XML into the TreeView.
                TreeView1.Nodes.Clear()
                TreeView1.Nodes.Add(New TreeNode(_rootNode))
                Dim node As New TreeNode()
                node = TreeView1.Nodes(0)
                Dim nodes As XmlNodeList = doc.SelectNodes(_XPath)
                Dim xnode As XmlNode = nodes.Item(0).ParentNode
                AddNode(xnode, node)
                TreeView1.ExpandAll()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
            MessageBox.Show("There is no addressbook available, please create one!!")
            TreeView1.Nodes.Add("root", "Worshipteam")
        End If
    End Sub

    Private Sub AddNode(xmlNode As XmlNode, treeNode As TreeNode)
        Dim faqNode As TreeNode
        Dim nodeList As XmlNodeList
        ' Loop through the XML nodes until the leaf is reached.
        ' Add the nodes to the TreeView during the looping process.
        If xmlNode.HasChildNodes Then
            nodeList = xmlNode.ChildNodes
            For Each node As XmlNode In nodeList
                faqNode = New TreeNode(node("FirstName").InnerText)
                faqNode.Nodes.Add(New TreeNode(node("FullName").InnerText))
                faqNode.Nodes.Add(New TreeNode(node("Emailaddress").InnerText))
                treeNode.Nodes.Add(faqNode)
            Next
        Else
            ' Here you need to pull the data from the XmlNode based on the
            ' type of node, whether attribute values are required, and so forth.
            treeNode.Text = (xmlNode.OuterXml).Trim()
        End If
    End Sub

As you can see it uses InnerText of the nodes.

Here is the text I have at the moment to save it, I created a module for it:
VB.NET:
Friend Module SaveTreeview
    Public Sub kcTree2XML(ByVal XMLFilePath As String, ByVal kcTreeview As  _
                            TreeView)
        Dim XMLDoc As New Xml.XmlDocument
        Dim RootNode As TreeNode
        Dim NewXMLNode As Xml.XmlNode
        Dim XMLAttribute As Xml.XmlAttribute

        XMLDoc.LoadXml(("<?xml version='1.0' ?>" & _
        "<Worshipteam><Name>" & _
        "</Name></Worshipteam>"))

        For Each RootNode In kcTreeview.Nodes
            NewXMLNode = XMLDoc.CreateNode(Xml.XmlNodeType.Element, "Node",
            "Node", "")
            XMLAttribute = XMLDoc.CreateAttribute("name")
            XMLAttribute.Value = RootNode.Text
            NewXMLNode.Attributes.Append(XMLAttribute)
            XMLDoc.DocumentElement.AppendChild(NewXMLNode)
            SaveChildNodes(NewXMLNode, RootNode, XMLDoc)
        Next

        XMLDoc.Save(XMLFilePath)

    End Sub

    Private Sub SaveChildNodes(ByVal XMLNode As Xml.XmlNode, ByVal _
                               ParentNode As TreeNode, ByVal XMLDoc As Xml.XmlDocument)
        Dim ChildNode As TreeNode
        Dim NewXMLNode As Xml.XmlNode
        Dim XMLAttribute As Xml.XmlAttribute

        For Each ChildNode In ParentNode.Nodes
            NewXMLNode = XMLDoc.CreateNode(Xml.XmlNodeType.Element, "Node",
            "Node", "")
            XMLAttribute = XMLDoc.CreateAttribute("name")
            XMLAttribute.Value = ChildNode.Text

            NewXMLNode.Attributes.Append(XMLAttribute)
            XMLNode.AppendChild(NewXMLNode)

            SaveChildNodes(NewXMLNode, ChildNode, XMLDoc)
        Next

    End Sub
End Module

But it saves the treeview as followed:
VB.NET:
<?xml version="1.0"?>
<Worshipteam>
  <Name>
  </Name>
  <Node name="Worshipteam">
      <Node name="firstname">
      <Node name="fullname" />
      <Node name="email" />
    </Node>
  </Node>
</Worshipteam>

Thanks
 
First problem, you create attributes and can't expect elements to emerge from that.
Second problem, you're using recursion and have no information about the xml element names to produce such output.
You actually have a flat layout here, root Worshipteam treenode with all Name elements as childs, so you can just loop over that.
To write the document a XmlTextWriter is much easier in use, but since you're on .Net 4 I will post an example using inline xml literals which is even easier:
Dim content As XElement = <Worshipteam>
                              <%= From node In TreeView1.Nodes(0).Nodes.Cast(Of TreeNode)() Select
                                  <Name>
                                      <FirstName><%= node.Text %></FirstName>
                                      <FullName><%= node.Nodes(0).Text %></FullName>
                                      <EmailAddress><%= node.Nodes(1).Text %></EmailAddress>
                                  </Name>
                              %>
                          </Worshipteam>

Dim decl As New XDeclaration("1.0", "iso-8859-1", "")
Dim doc As New XDocument(decl, content)
doc.Save(XMLFilePath)
 
Back
Top