Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can save and load all child nodes (.nodes) from the selected node, but not the childs of childs etc.

Tried many examples with no luck.

I don't really understand how to serialize the structure of the tree and data, then load the data back into the structure?

Anyone have a sweet example that does this?
Posted
Updated 20-Nov-12 16:11pm
v2

 
Share this answer
 
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.

C#
[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

C#
public class PrepareTree
{
    
    /// <summary>
    /// This function prepares a tree node and all child nodes
    /// </summary>
    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;
    }

    /// <summary>
    /// This functions returns a tree node and all child nodes
    /// </summary>
    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)
    {
        //Prepare children
        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.

C#
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);
            }
        }
    }
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900