• 序列化


    一、序列化的目的:

    使用文件存储时,只存储了对象数据,再读取后,就难以区分出原有的数据类型。

    目的:将整个对象序列化为字节流写入文件,文件中保存了原有属性的数据类型,并能够在读取出的内容,反序列为对象。

    二、序列化与反序列化主要语句

    1、对象标识序列化,可序列化的内容有:class、enum、struct、delegate。

    [Serializable]
    public class Student

    2、序列化操作

    //操作对象序列化的类
    BinaryFormatter formatter = new BinaryFormatter();

    FileStream output=new FileStream(fileFullName,FileMode.OpenOrCreate)

    //序列化

    formatter.Serialize(output, lst);

    //反序列化

    List<Student> lstStudents = (List<Student>)formatter.Deserialize(fs);

    三、示例代码

      1 namespace 序列化
      2 {
      3     class Program
      4     {
      5         static void Main(string[] args)
      6         {
      7             List<Student> lst = new List<Student>();
      8             //操作对象序列化的类
      9             BinaryFormatter formatter = new BinaryFormatter();
     10             //序列化后,存放的文件位置
     11             string fileFullName="c:\students.txt";
     12             
     13                  
     14             #region 模拟数据
     15             StudentClass c = new StudentClass();
     16             c.ClassID = 1;
     17             c.ClassName = "大一";
     18             c.MainTeacher = "秋飘";
     19 
     20             Student s1 = new Student();
     21             s1.SID = 1;
     22             s1.SName = "张三";
     23             s1.SAge = 23;
     24             s1.StudenClass = c;
     25             lst.Add(s1);
     26 
     27             Student s2 = new Student();
     28             s2.SID = 2;
     29             s2.SName = "李四";
     30             s2.SAge = 20;
     31             s2.StudenClass = c;
     32             lst.Add(s2);
     33             #endregion
     34 
     35             try
     36             {
     37                 using(FileStream output=new FileStream(fileFullName,FileMode.OpenOrCreate))
     38                 {
     39                     Console.WriteLine(string.Format("开始序列化对象到文件:{0}中",output.Name));
     40                     formatter.Serialize(output, lst);
     41                     Console.WriteLine("序列化完毕!");
     42                     Console.WriteLine("
    ");
     43                 }
     44                 
     45                using(StreamReader fs = new StreamReader(fileFullName))
     46                {
     47                    Console.WriteLine(new string('-', 50));
     48                    string serializeResult = fs.ReadToEnd();
     49                    Console.WriteLine(serializeResult);
     50                    Console.WriteLine(new string('-', 50));
     51                    Console.WriteLine("
    ");
     52                }
     53 
     54                using (FileStream fs = new FileStream(fileFullName, FileMode.Open, FileAccess.Read))
     55                {
     56                    Console.WriteLine("开始反序列化,读取文件内容。");
     57                    List<Student> lstStudents = (List<Student>)formatter.Deserialize(fs);
     58                    if (lstStudents.Count != null)
     59                    {
     60                        Console.WriteLine(string.Format("反序列化后,Student的个数:{0},如下:",lstStudents.Count));
     61                        foreach (Student s in lstStudents)
     62                        {
     63                            Console.WriteLine(string.Format("姓名:{0},年龄:{1},年级:{2}", s.SName, s.SAge,s.StudenClass.ClassName));
     64                        }
     65                    }
     66                }
     67 
     68             }
     69             catch(SerializationException se)
     70             {
     71                 Console.WriteLine("序列化异常:" + se.Message);
     72             }
     73             catch (Exception ex)
     74             {
     75                 Console.WriteLine("其它异常:"+ex.Message);
     76             }
     77 
     78             Console.ReadLine();
     79         }
     80     }
     81 
     82     /// <summary>
     83     /// 设置学生类是可以序列化的
     84     /// </summary>
     85     [Serializable]
     86     public class Student
     87     {
     88         
     89         private int m_SID;
     90         /// <summary>
     91         /// 学生ID
     92         /// </summary>
     93         public int SID
     94         {
     95             get { return m_SID; }
     96             set { m_SID = value; }
     97         }
     98         private string m_SName;
     99         /// <summary>
    100         /// 学生姓名
    101         /// </summary>
    102         public string SName
    103         {
    104             get { return m_SName; }
    105             set { m_SName = value; }
    106         }
    107 
    108         private int m_SAge;
    109         /// <summary>
    110         /// 年龄
    111         /// </summary>
    112         public int SAge
    113         {
    114             get { return m_SAge; }
    115             set { m_SAge = value; }
    116         }
    117 
    118         
    119         private StudentClass m_StudentClass;
    120         /// <summary>
    121         /// 班级
    122         /// </summary>
    123         
    124         public StudentClass StudenClass
    125         {
    126             get { return m_StudentClass; }
    127             set { m_StudentClass = value; }
    128         }
    129 
    130 
    131     }
    132 
    133     [Serializable]
    134     public class StudentClass
    135     {
    136         private int m_ClassID;
    137         /// <summary>
    138         /// 班级ID
    139         /// </summary>
    140         public int ClassID
    141         {
    142             get { return m_ClassID; }
    143             set { m_ClassID = value; }
    144         }
    145 
    146         private string m_ClassName;
    147         /// <summary>
    148         /// 班级名称
    149         /// </summary>
    150         public string ClassName
    151         {
    152             get { return m_ClassName; }
    153             set { m_ClassName = value; }
    154         }
    155 
    156         private string m_MainTeacher;
    157         /// <summary>
    158         /// 班主任
    159         /// </summary>
    160         public string MainTeacher
    161         {
    162             get { return m_MainTeacher; }
    163             set { m_MainTeacher = value; }
    164         }
    165 
    166     }
    167 }

    四、序列化后的文件内容如下:

  • 相关阅读:
    ASP.NET(C#)图片加文字、图片水印
    CMake构建Visual Studio中MFC项目的Unicode问题
    用Visual Studio 2008(VS)编译WebKit的r63513
    此时学习中
    ASP.NET进阶——初学者的提高(长期)
    继续努力
    程序员阿士顿的故事
    iOS 深拷贝和浅拷贝
    Javascript中this的取值
    Lisp的本质(The Nature of Lisp)
  • 原文地址:https://www.cnblogs.com/qiupiaohujie/p/11962446.html
Copyright © 2020-2023  润新知