views:

100

answers:

2

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.

A: 

How do you generate the keys for the nodes? Since keys must be unique, if you use a scheme that includes parent identification, you should be able to reconstruct the hierarchy from them.

Jim Mack
The keys are already generated, I just wanted to serialize the nodes and then deserialize them.
ajax
A: 

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!

Cody Gray