How can we serialize treeview nodes in VB6?
I am unable to figure out how to traverse and preserve the relationships between the nodes for serialization.
2 Answers
In VB6, I used to serialize treeview nodes to XML files. This is easily readable back into the program, and has the additional advantage of being a completely human-readable and editable format. Taking advantage of XML's nested format allows you to easily preserve the relationship between the nodes. Generally, I loaded the XML file into the treeview all at one time, and then traversed from its Nodes collection, but you can also traverse the XML file using the DOM built into the MSXML parser.
The only downside is introducing a dependency on the MSXML parser, but if you already have a setup routine in place, this shouldn't be a big deal. Additionally, MSXML 3.0 is almost universally deployed, thanks to Internet Explorer, if that's a concern for you. Keep in mind that 3.0 is the last version to run on the Windows 9x/ME platforms, but it's getting pretty old now, and Microsoft recommends the use of MSXML 6.0 on anything newer.
Check out this link (specifically the "Save Nodes (Nested)", rather than the one that creates a 'flat' XML file) for an example of this approach: http://www.devx.com/vb/Article/9707/0/page/4. There's an example project you can download, I think, but the logic is really not very complex. Make sure you add a reference to MSXML in the IDE!