Implement all 3 sections, and the load and save functions save/load all nodes and child nodes and all data associated with those nodes. All nodes should derive from the basenode class. It works.
Section 1:
Create a serializable treenode that contains treenode List<>. The list is used to store child nodes.
[Serializable]
public class BaseNode : TreeNode, ISerializable
{
List<TreeNode> _nodelist = new List<TreeNode>();
public List<TreeNode> Nodelist
{
get { return _nodelist; }
set { _nodelist = value; }
}
public BaseNode(SerializationInfo info, StreamingContext ctxt)
{
_nodelist = (List<TreeNode>)info.GetValue("Nodes", typeof(List<TreeNode>));
}
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
protected virtual void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Nodes", this._nodelist, typeof(List<TreeNode>));
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
GetObjectData(info, ctxt);
}
}
Section 2:
The PrepareTree takes the node passed and wraps up all the nodes and data in the returned node
public class PrepareTree
{
public static TreeNode Write(BaseNode startnode)
{
try
{
TreeNode Final = WfnPrepareTreeNode(startnode);
return Final;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private static List<TreeNode> WfnPrepareChildNode(TreeNode tr)
{
List<TreeNode> ListTreeNode = new List<TreeNode>();
TreeNode stc;
foreach (BaseNode trc in tr.Nodes)
{
stc = WfnPrepareTreeNode(trc);
ListTreeNode.Add(stc);
}
return ListTreeNode;
}
private static TreeNode WfnPrepareTreeNode(BaseNode tr)
{
tr.Nodelist = WfnPrepareChildNode(tr);
return tr;
}
public static TreeNode Read(BaseNode TreeNode)
{
try
{
TreeNode FinalTreeNode = RfnPrepareTreeNode(TreeNode);
return FinalTreeNode;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private static List<TreeNode> RfnPrepareChildNodes(BaseNode str)
{
List<TreeNode> retTreeNode = new List<TreeNode>();
TreeNode tnc;
foreach (BaseNode strc in str.Nodelist)
{
tnc = RfnPrepareTreeNode(strc);
retTreeNode.Add(tnc);
}
return retTreeNode;
}
private static TreeNode RfnPrepareTreeNode(BaseNode str)
{
List<TreeNode> retTempTreeNodeList = RfnPrepareChildNodes(str);
foreach (BaseNode tempTr in retTempTreeNodeList)
{
str.Nodes.Add(tempTr);
}
return str;
}
}
Section 3:
Simply call SaveTree and pass it the choosen node, and all nodes and child nodes will be saved. Same with LoadTree.
public static class SerializeTree
{
public static void SaveTree(BaseNode node, string filename)
{
using (Stream file = File.Open(filename, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
TreeNode savenode = PrepareTree.Write(node);
bf.Serialize(file, savenode);
}
}
public static void LoadTree(BaseNode node, string filename, TreeView tree)
{
try
{
BinaryFormatter bin = new BinaryFormatter();
FileStream fTree = new FileStream(filename, FileMode.Open, FileAccess.Read);
BaseNode str = (BaseNode)bin.Deserialize(fTree);
fTree.Close();
TreeNode trParent = PrepareTree.Read(str);
node.Nodes.Add(trParent);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message, "Load Tree Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}