Serialize/Deserialize TreeView

  • Thread starter Thread starter Bamse
  • Start date Start date
B

Bamse

Hi, I need to serialize/deserialize a treeview in order to retain its
nodes/values.
the serialization worked;
using soapformatter:

System.Runtime.Serialization.Formatters.Soap.SoapFormatter _sf = new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
string _filePath = "tree.xml";
TreeNode _RootNode = new TreeNode();
foreach (TreeNode _tn in this.TreeView.Nodes)
{
_RootNode.Nodes.Add((TreeNode)_tn.Clone());
}
FileStream _fs = new FileStream(_filePath, FileMode.Create,
FileAccess.Write, FileShare.None);
_sf.Serialize(_fs, _RootNode);
_fs.Close();

but at the deserialization I get an "Specified cast is not valid" exception


System.Runtime.Serialization.Formatters.Soap.SoapFormatter _sf = new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
string _filePath = "tree.xml";
_fs = new FileStream(_filePath, FileMode.Open);
TreeNode _Root = null;
_Root = (TreeNode) _sf.Deserialize(_fs); // here is thrown
the exception
_fs.Close();
this.TreeView.Nodes.Add(_Root);

one more thing, the Tag property of each TreeNode contains a custom class
which has 4 fields filled with information and I made it Serializable and
implemented ISerializable.
the TreeNode is serializable, my class is also serializable, yet the
deserialization does not work :(
Any ideas how to make it work?

Thank you
 
Back
Top