Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 20-Nov-12 15:45pm
mbos015319
Edited 20-Nov-12 16:11pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
{
    
    /// <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.
 
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);
            }
        }
    }
  Permalink  
v4

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

  Print Answers RSS
0 OriginalGriff 230
1 Sergey Alexandrovich Kryukov 104
2 Richard MacCutchan 100
3 kbrandwijk 75
4 Vinay Mistry 70
0 Sergey Alexandrovich Kryukov 9,040
1 OriginalGriff 8,121
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100