保存&将所有TreeView节点加载到所选节点下面的文件(序列化) [英] Save & Load All TreeView Nodes Below Selected Node To File (serialize)

查看:70
本文介绍了保存&将所有TreeView节点加载到所选节点下面的文件(序列化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以保存并加载所选节点中的所有子节点(.nodes),但不能保存子节点等子节点。



尝试了很多没有运气的例子。



我真的不明白如何序列化树和数据的结构,然后将数据加载回结构?



任何人都有一个很好的例子吗?

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?

推荐答案

树遍历 [ ^ ]


实现所有3个部分,加载和保存功能保存/加载所有节点和子节点以及与这些节点关联的所有数据。所有节点都应该从basenode类派生。它有效。



第1部分:

创建一个包含treenode List<>的可序列化treenode。该列表用于存储子节点。



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);
    }
}





第2节:

PrepareTree接受节点传递并包装返回节点中的所有节点和数据





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;
    }
}





第3部分:

只需致电SaveTree并传递给它选择节点,将保存所有节点和子节点。与LoadTree相同。





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);
            }
        }
    }


这篇关于保存&amp;将所有TreeView节点加载到所选节点下面的文件(序列化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆