1.序列化一般有2种(XML和2进制),简单对象序列化

  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.Windows.Forms;
  5. using System.Xml.Serialization;
  6.  
  7. namespace Test
  8. {
  9. //对于XmlSerializer序列化,默认即使不使用特性Serializable,也是可以对对象进行序列化的,则BinaryFormatter不然一定要使用Serializable标记。
  10. public partial class Form1 : Form
  11. {
  12. //XmlSerializer是XML序列化
  13. XmlSerializer xs = new XmlSerializer(typeof(Student));
  14. //二进制序列化
  15. BinaryFormatter b = new BinaryFormatter();
  16. Student student = new Student() { Name = "小明", Age = };
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. //xml序列化
  21. using (Stream stream = new FileStream("d:\\Student.xml", FileMode.Create, FileAccess.Write, FileShare.Read))
  22. {
  23. xs.Serialize(stream, student);
  24. }
  25. //xml反序列化
  26. using (FileStream fs = new FileStream("d:\\Student.xml", FileMode.Open, FileAccess.Read))
  27. {
  28. Student student = (Student)xs.Deserialize(fs);
  29. }
  30. //二进制序序列化
  31. using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create))
  32. {
  33. BinaryFormatter b = new BinaryFormatter();
  34. //序列化类要加[Serializable]特性
  35. b.Serialize(fileStream, student);
  36. }
  37. //二进制序反序列化
  38. using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read))
  39. {
  40. BinaryFormatter bf = new BinaryFormatter();
  41. student = (Student)bf.Deserialize(fileStream);
  42. }
  43. }
  44.  
  45. }
  46. }
  47.  
  48. [Serializable]
  49. public class Student
  50. {
  51. public string Name { get; set; }
  52. public int Age { get; set; }
  53. }

2.复杂对象序列化

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Xml.Serialization;
  13.  
  14. namespace Test
  15. {
  16. public partial class Form1 : Form
  17. {
  18. //XmlSerializer是XML序列化
  19. XmlSerializer xs = new XmlSerializer(typeof(TeacherStudent));
  20. //二进制序列化
  21. BinaryFormatter b = new BinaryFormatter();
  22. TeacherStudent teacherStudent = new TeacherStudent();
  23.  
  24. Teacher teacher = new Teacher() { Name = "王老师", Age = };
  25. Student student = new Student() { Name = "小明", Age = };
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. //xml序列化
  30. using (Stream stream = new FileStream("d:\\Student.xml", FileMode.Create, FileAccess.Write, FileShare.Read))
  31. {
  32. teacherStudent.Teacher = teacher;
  33. teacherStudent.Student = student;
  34. xs.Serialize(stream, teacherStudent);
  35. }
  36. //xml反序列化
  37. using (FileStream fs = new FileStream("d:\\Student.xml", FileMode.Open, FileAccess.Read))
  38. {
  39. teacherStudent = null;
  40. teacherStudent = (TeacherStudent)xs.Deserialize(fs);
  41. }
  42. //二进制序序列化
  43. using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create))
  44. {
  45. BinaryFormatter b = new BinaryFormatter();
  46. b.Serialize(fileStream, teacher);
  47. b.Serialize(fileStream, student);
  48. }
  49. //二进制序反序列化
  50. using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read))
  51. {
  52. teacher = null;
  53. student = null;
  54. BinaryFormatter bf = new BinaryFormatter();
  55. teacher = (Teacher)bf.Deserialize(fileStream);
  56. student = (Student)bf.Deserialize(fileStream);
  57. }
  58. }
  59.  
  60. }
  61. }
  62.  
  63. [Serializable]
  64. public class Student
  65. {
  66. public string Name { get; set; }
  67. public int Age { get; set; }
  68. }
  69.  
  70. [Serializable]
  71. public class Teacher
  72. {
  73. public string Name { get; set; }
  74. public int Age { get; set; }
  75. }
  76.  
  77. [Serializable]
  78. public class TeacherStudent
  79. {
  80. public Teacher Teacher { get; set; }
  81. public Student Student { get; set; }
  82. }

3. 控制序列化/反序列化前后的数据

  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Windows.Forms;
  6.  
  7. namespace Test
  8. {
  9. public partial class Form1 : Form
  10. {
  11. //XmlSerializer是XML序列化
  12. BinaryFormatter b = new BinaryFormatter();
  13. Student student = new Student() { Name = "小明", Age = };
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. //二进制序序列化
  18. using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Create))
  19. {
  20. BinaryFormatter b = new BinaryFormatter();
  21. b.Serialize(fileStream, student);
  22. }
  23. //二进制序反序列化
  24. using (FileStream fileStream = new FileStream("d:\\Student.dat", FileMode.Open, FileAccess.Read))
  25. {
  26. BinaryFormatter bf = new BinaryFormatter();
  27. student = (Student)bf.Deserialize(fileStream);
  28. }
  29. }
  30.  
  31. }
  32. }
  33.  
  34. [Serializable]
  35. public class Student
  36. {
  37. public string Name { get; set; }
  38. public int Age { get; set; }
  39.  
  40. [OnSerializing()]
  41. internal void OnSerializingMethod(StreamingContext context)
  42. {
  43. //格式化器在序列化开始之前调用此方法。
  44. Console.WriteLine("OnSerializing格式化器在序列化开始之前调用此方法");
  45. }
  46.  
  47. [OnSerialized()]
  48. internal void OnSerializedMethod(StreamingContext context)
  49. {
  50. //格式化器在序列化后调用此方法。
  51. Console.WriteLine("OnSerialized格式化器在序列化后调用此方法");
  52. }
  53.  
  54. [OnDeserializing()]
  55. internal void OnDeserializingMethod(StreamingContext context)
  56. {
  57. //格式化器在反序列化开始之前调用此方法。
  58. Console.WriteLine("OnDeserializing格式化器在反序列化开始之前调用此方法");
  59. }
  60.  
  61. [OnDeserialized()]
  62. internal void OnDeserializedMethod(StreamingContext context)
  63. {
  64. //格式化器在序列化开始之前调用此方法。
  65. Console.WriteLine("OnDeserialized格式化器在序列化开始之前调用此方法");
  66. }
  67. }

C#序列化的更多相关文章

  1. 【.net 深呼吸】序列化中的“引用保留”

    假设 K 类中有两个属性/字段的类型相同,并且它们引用的是同一个对象实例,在序列化的默认处理中,会为每个引用单独生成数据. 看看下面两个类. [DataContract] public class 帅 ...

  2. 【.net 深呼吸】设置序列化中的最大数据量

    欢迎收看本期的<老周吹牛>节目,由于剧组严重缺钱,故本节目无视频无声音.好,先看下面一个类声明. [DataContract] public class DemoObject { [Dat ...

  3. 用dubbo时遇到的一个序列化的坑

    首先,这是标题党,问题并不是出现在序列化上,这是报错的一部分: Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to s ...

  4. Unity 序列化

    Script Serialization http://docs.unity3d.com/Manual/script-Serialization.html 自定义序列化及例子: http://docs ...

  5. Unity 序列化 总结

    查找了 Script Serialization http://docs.unity3d.com/Manual/script-Serialization.html 自定义序列化及例子: http:// ...

  6. [C#] C# 知识回顾 - 序列化

    C# 知识回顾 -  序列化 [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5902005.html 目录 序列化的含义 通过序列化保存对象数据 众 ...

  7. Newtonsoft.Json设置类的属性不序列化

    参考页面: http://www.yuanjiaocheng.net/webapi/parameter-binding.html http://www.yuanjiaocheng.net/webapi ...

  8. C# 序列化与反序列化几种格式的转换

    这里介绍了几种方式之间的序列化与反序列化之间的转换 首先介绍的如何序列化,将object对象序列化常见的两种方式即string和xml对象; 第一种将object转换为string对象,这种比较简单没 ...

  9. Netty实现高性能RPC服务器优化篇之消息序列化

    在本人写的前一篇文章中,谈及有关如何利用Netty开发实现,高性能RPC服务器的一些设计思路.设计原理,以及具体的实现方案(具体参见:谈谈如何使用Netty开发实现高性能的RPC服务器).在文章的最后 ...

  10. .Net深入实战系列—JSON序列化那点事儿

    序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...

随机推荐

  1. Angularjs -Promise - $http

    https://www.peterbe.com/plog/promises-with-$http

  2. python笔记1-转义字符

    print(r'dd"e"f')print(r'dd'e'f')print(r"dd"e"f")print(r"dd'e'f&qu ...

  3. Reset Identity Column Value in SQL Server (Identity Reset)

    前言:今天在群里看到有人在问SQL Server自增值重置问题(sqlserver identiy column value reset ) 闲话少说,直接上代码: 正文: --create tabl ...

  4. widows和Linux java加密注意事项

    /** * @Title: EncrypAES.java * @Package com.weidinghuo.payment.util * @Description: TODO(用一句话描述该文件做什 ...

  5. python包使用指南-创建虚拟环境

    创建虚拟环境的两种方法: 1.virtualenv 2.venv http://packaging.python.org/en/latest/installing/#creating-virtual- ...

  6. Java学习笔记-Math类

    并非所有的类都需要main方法.Math类和JOptionPane类都没有main方法.这些类中所包含的方法主要是为了供其他类使用. package welcome; public class Tes ...

  7. 2.3switch case 语句注意事项。

    #include<stdio.h> int main() { void action1(int, int),action2(int, int); char ch; , b=; ch = g ...

  8. Oracle to_date()函数的用法

    Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...

  9. SPSS课程学习思路及流程

    数据挖掘领域对行的分析

  10. vtkQuadric创建二次曲面

    在本实例中,我们将用到vtkQuadric.vtkSampleFunction.vtkContourFilter三个类,分别是二次曲面函数.函数曲面抽样和等高滤波. vtkQuadric负责二次曲面基 ...