zoukankan      html  css  js  c++  java
  • C# TreeView序列化、串行化、反序列化

    原文地址

    2009年给公司做了个管理系统,里面用到了TreeView,数据有1500多条,加载要10秒钟左右

    秉着有问题,就要解决,尤其是像这种几十人、上百人使用的系统。

    后来使用登录时加载数据,登录后再使用就Clone()的解决方案,感觉不错。不过,每次调试程序时得等半天,实在不爽。

    前些天看到“序列化”这个概念,大意就是把对象保存为一个文件,下次再使用时,反序列化一下就OK了,第二天一大早到公司立马做了个Demo,哇,不错,非常之不错,使用此方法后,根本没有延迟的现象。今天终于应用到这个项目中了,同志们再也不用等那10秒了。

     

    以上是背景,将其上杂乱的代码进行整理,得到整理好的对Table、TreeView序列化的类,版本1.0。

     

    using System;
    using System.IO;
    using System.Windows.Forms;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Data;
    
    
    namespace DBFSR.HRDRW
    {
    
        /// <summary>
        /// 对TreeView,DataTable 对象进行序列化的存储
        /// 
        /// Usage:
        /// 反序列化:SerializeTree.TreeViewDataAccess.LoadTreeViewData(treeView1,"C:\treeview.txt");  
        /// 序列化:  SerializeTree.TreeViewDataAccess.SaveTreeViewData(treeView1,"C:\treeview.txt");  
        /// </summary>
        class TreeView_Serialize
        {
            public TreeView_Serialize()
            {
    
            }
    
            [Serializable()]
            public struct TreeViewData
            {
                public TreeNodeData[] Nodes;
    
                /// <summary>  
                /// 递归初始化TreeView数据  
                /// </summary>  
                /// <param name="treeview"></param>  
                public TreeViewData(TreeView treeview)
                {
                    Nodes = new TreeNodeData[treeview.Nodes.Count];
                    if (treeview.Nodes.Count == 0)
                    {
                        return;
                    }
                    for (int i = 0; i <= treeview.Nodes.Count - 1; i++)
                    {
                        Nodes[i] = new TreeNodeData(treeview.Nodes[i]);
                    }
                }
    
                // <summary>
                /// 弹出TreeView
                /// </summary>
                /// <param name="tv"></param>
                public void PopupTree(TreeView tv)
                {
    
                    if (this.Nodes == null || this.Nodes.Length == 0)
                    {
                        return;
                    }
                    tv.BeginUpdate();
                    for (int i = 0; i <= this.Nodes.Length - 1; i++)
                    {
                        tv.Nodes.Add(this.Nodes[i].GetTreeNodes());
                    }
                    tv.EndUpdate();
    
                }
    
            }
    
            /// <summary>
            /// 对TreeNode数据对象进行序列化
            /// </summary>
            [Serializable()]
            public struct TreeNodeData
            {
                public string m_strText;
                public int m_nImageIndex;
                public int m_nSelectedImageIndex;
    
                public bool m_bChecked;
                public bool m_bExpanded;
                public object m_Tag;
    
                public TreeNodeData[] m_Nodes;
    
    
                /// <summary>  
                /// TreeNode构造函数  
                /// </summary>  
                /// <param name="tn">传入初始化内容</param>  
                public TreeNodeData(TreeNode tn)
                {
                    this.m_strText = tn.Text;
                    this.m_nImageIndex = tn.ImageIndex;
                    this.m_nSelectedImageIndex = tn.SelectedImageIndex;
                    this.m_bChecked = tn.Checked;
                    this.m_bExpanded = tn.IsExpanded;
                    this.m_Nodes = new TreeNodeData[tn.Nodes.Count];
    
                    if ((!(tn.Tag == null)) && tn.Tag.GetType().IsSerializable)
                        this.m_Tag = tn.Tag;
                    else
                        this.m_Tag = null;
    
                    if (tn.Nodes.Count == 0)
                        return;
    
                    for (int i = 0; i <= tn.Nodes.Count - 1; i++)
                        m_Nodes[i] = new TreeNodeData(tn.Nodes[i]);
    
                }
    
                /// <summary>
                /// 返回TreeNode数据
                /// </summary>
                /// <returns></returns>
                public TreeNode GetTreeNodes()
                {
                    TreeNode ToTreeNode = new TreeNode(this.m_strText, this.m_nImageIndex, this.m_nSelectedImageIndex);
                    ToTreeNode.Checked = this.m_bChecked;
                    ToTreeNode.Tag = this.m_Tag;
                    if (this.m_bExpanded)
                    {
                        ToTreeNode.Expand();
                    }
                    if (this.m_Nodes == null && this.m_Nodes.Length == 0)
                    {
                        return null;
                    }
                    if (ToTreeNode != null && this.m_Nodes.Length == 0)
                    {
                        return ToTreeNode;
                    }
                    for (int i = 0; i <= this.m_Nodes.Length - 1; i++)
                    {
                        ToTreeNode.Nodes.Add(this.m_Nodes[i].GetTreeNodes());
                    }
                    return ToTreeNode;
    
    
                }
            }
    
            /// <summary>
            /// 加载TreeView
            /// </summary>
            /// <param name="treeView"></param>
            /// <param name="path"></param>
            public static void LoadTreeViewData(TreeView treeView, string path)
            {
    
                BinaryFormatter bfmter = new BinaryFormatter();
                Stream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                TreeViewData treeData = ((TreeViewData)(bfmter.Deserialize(file)));
                treeData.PopupTree(treeView);
                file.Close();
    
            }
    
            /// <summary>
            /// 保存TreeView到文件
            /// </summary>
            /// <param name="treeView"></param>
            /// <param name="path"></param>
            public static void SaveTreeViewData(TreeView treeView, string path)
            {
                BinaryFormatter ser = new BinaryFormatter();
                Stream file = new FileStream(path, FileMode.Create);
                ser.Serialize(file, new TreeViewData(treeView));
                file.Close();
    
            }
    
    
        }
    
    
        /// <summary>
        /// 序列化,及反序列化DataTable
        /// </summary>
        class DataTable_Serialize
        {
            public static DataTable LoadDataTable(string path)
            {
    
                BinaryFormatter ser = new BinaryFormatter();
                Stream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                DataTable dt = (DataTable)ser.Deserialize(file);
                file.Close();
                return dt;
            }
    
            public static void SaveDataTable(DataTable dt, string path)
            {
                BinaryFormatter ser = new BinaryFormatter();
                Stream file = new FileStream(path, FileMode.Create);
                ser.Serialize(file, dt);
                file.Close();
    
            }
        }
    
    }
    View Code
    你们的评论、反馈,及对你们有所用,是我整理材料和博文写作的最大的鼓励和唯一动力。欢迎讨论和关注!
    没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。
  • 相关阅读:
    Git之GitFlow工作流
    auth
    rbac
    修改或删除windows上的git用户凭证信息
    Linux之shell备份数据库
    解决Navicat 连接服务器数据库报10060问题
    PHP框架学习思路
    shell之hello world
    Linux命令大全
    PHP 各个版本的区别
  • 原文地址:https://www.cnblogs.com/arxive/p/14320739.html
Copyright © 2011-2022 走看看