Serialize TreeView
-
I have a custom treeview class with a couple of extra properties and methods. When I try to serialize an object of that type, I get a compiler error saying that TreeView has not been marked as serializable. Any suggestions as to how I can serialize this object? Any help would be greatly appreciated.
-
I have a custom treeview class with a couple of extra properties and methods. When I try to serialize an object of that type, I get a compiler error saying that TreeView has not been marked as serializable. Any suggestions as to how I can serialize this object? Any help would be greatly appreciated.
If you implement ISerializable you can control how the serialization happens; so that instead of serializing a TreeView; you serialize the parts you need to recreate it (ie the nodes, and your custom property). If the nodes aren't serializable, you'll have to do something special with them too. James "Java is free - and worth every penny." - Christian Graus
-
If you implement ISerializable you can control how the serialization happens; so that instead of serializing a TreeView; you serialize the parts you need to recreate it (ie the nodes, and your custom property). If the nodes aren't serializable, you'll have to do something special with them too. James "Java is free - and worth every penny." - Christian Graus
I gave up on serializing the treeview, instead I just made a class with the same properties and meathod as before. The class has one extra property: a TreeNode. It is essentially just a root node for everything that would have been in the treeview. I implemented ISerializable like you said, and it serializes and deserializes perfectly except when the TreeNode has any other nodes in it (doh!). Do you know how I can get around this? Thanks for the help, Steve
-
If you implement ISerializable you can control how the serialization happens; so that instead of serializing a TreeView; you serialize the parts you need to recreate it (ie the nodes, and your custom property). If the nodes aren't serializable, you'll have to do something special with them too. James "Java is free - and worth every penny." - Christian Graus
-
One more thing I thought of. I could recurse through the nodes. I would have to first serialize all the 'sub'nodes then clear and serialize the node itself. My problem then is: How do I deserialize them?
All you should have to do is Serialize the root node; it should then serialize everything referenced in that node; including its child nodes. When you deserialize only worry about the first node; the rest should come with it. James "Java is free - and worth every penny." - Christian Graus
-
All you should have to do is Serialize the root node; it should then serialize everything referenced in that node; including its child nodes. When you deserialize only worry about the first node; the rest should come with it. James "Java is free - and worth every penny." - Christian Graus
Try running this (and look at the y and z definitions): using System.IO; using System.Runtime; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters.Binary; . . . TreeNode x,y,z; x = new TreeNode("Root"); y = x.Nodes.Add("A"); //it only runs if you comment out these lines z = y.Nodes.Add("1"); //it only runs if you comment out these lines IFormatter formatter = new BinaryFormatter(); Stream mystream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(mystream, x); mystream.Close(); TreeNode root; mystream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read); root = (TreeNode)formatter.Deserialize(mystream); MessageBox.Show(root.Text); Thanks for the help :)