Question : Treeview to XML code - not getting sub nodes correctly

For some reason this XML code isn't parsing my treeview correctly. It stops after the first node.
I've added a For Next loop around the ie.MoveNext and it willl at least get all the nodes but its not putting them into XML correctly.

Its some code that I pulled off www.codeproject.com/KB/cpp/treeviewtoxml.aspx and converted to VB.net from C#.

Any help would be appreciated. Next question will be "Once the treeview contents are successfully saved, how do I read them back into the treeview on form load"
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
Imports System
Imports System.Collections
Imports System.IO
Imports System.Windows.Forms
 
Public Class TreeViewToXML
    Private Shared xmlstring As String = ""
    Private Shared sr As StreamWriter
 
 
    Public Sub New()
    End Sub
 
    '''  
    ''' Exports the given TreeView to Xml and returns a string containing all tags 
    '''  
    ''' TreeView - This TreeView will be parsed 
    ''' 
    Public Shared Sub exportToXml(ByVal tv As TreeView, ByVal filename As String)
        sr = New StreamWriter(filename, False, System.Text.Encoding.UTF8)
        sr.WriteLine("")
 
        Dim ie As IEnumerator = tv.Nodes.GetEnumerator()
 
        'If ie.MoveNext() Then
        '    Dim tn As TreeNode = DirectCast(ie.Current, TreeNode)
        '    sr.WriteLine("<" + tn.Text + ">")
        '    parseNode(tn)
        'End If
 
        For index As Integer = 1 To tv.Nodes.Count
            If ie.MoveNext() Then
                Dim tn As TreeNode = DirectCast(ie.Current, TreeNode)
                sr.WriteLine("<" + tn.Text + ">")
                parseNode(tn)
            End If
        Next
 
        sr.Close()
    End Sub
 
    Private Shared Sub parseNode(ByVal tn As TreeNode)
        Dim ie As IEnumerator = tn.Nodes.GetEnumerator()
 
        Dim parentnode As String = ""
 
        parentnode = tn.Text
        'For index As Integer = 1 To tn.Nodes.Count
        While ie.MoveNext()
            Dim ctn As TreeNode = DirectCast(ie.Current, TreeNode)
 
            If ctn.GetNodeCount(True) = 0 Then
                sr.Write(ctn.Text)
            Else
                sr.Write("<" + ctn.Text + ">")
            End If
            If ctn.GetNodeCount(True) > 0 Then
                parseNode(ctn)
            End If
        End While
 
        sr.Write("")
        sr.WriteLine("")
        'Next
    End Sub
End Class
 
 
 
'Namespace TreeViewExport
'        For index As Integer = 1 To tv.Nodes.Count
'            If ie.MoveNext() Then
'Dim tn As TreeNode = DirectCast(ie.Current, TreeNode)
'                sr.WriteLine("<" + tn.Text + ">")
'                parseNode(tn)
'            End If
'        Next
'End Namespace
 
 
 
'Code placed in main form
 
#Region "TREEVIEW TO XML"
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        TreeViewToXML.exportToXml(Me.tvMainJumpMenu, Application.StartupPath + "\test.xml")
        Me.RichTextBox1.LoadFile(Application.StartupPath + "\test.xml", RichTextBoxStreamType.PlainText)
    End Sub
#End Region

Answer : Treeview to XML code - not getting sub nodes correctly

OK - I think there needs to be a little recusion to make this work....   could be wrong :)

Create a function to call that excepts a TreeNode ...


and from inside the method call the method itself if the node haschildren, this will allow eachnode to be populated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
void PopulateTree( TreeNode node )
{
//populate / do work
 
if(node.Haschildren....)
PopulateTree( node.getchild???? or ?? )
// hope you get the idea.
 
}
Random Solutions  
 
programming4us programming4us