初识序列化和反序列化,使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化
序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程。对象一旦被序列化,就可以把对象状态保存到硬盘的某个位置,甚至还可以通过网络发送给另外一台机器上运行的进程。本篇主要包括:
● 使用BinaryFormatter类进行序列化和反序列化
● 使用ISerializable接口自定义序列化过程
● 使用XmlSerializer类进行序列化和反序列化
□ 使用BinaryFormatter类进行序列化和反序列化
首先把需要序列化的类打上[Serializable]特性,如果某个字段不需要被序列化,就打上[NonSerialized]特性。
[Serializable]public class Meeting{public string _name;[NonSerialized]public string _location;public Meeting(string name, string location){this._name = name;this._location = location;}}
对象序列化后需要一个载体文件,以下的Meeting.binary文件用来存储对象的状态。
static void Main(string[] args){Meeting m1 = new Meeting("年终总结","青岛");Meeting m2;//先序列化SerializedWithBinaryFormatter(m1,"Meeting.binary");m2 = (Meeting) DeserializeWithBinaryFormatter("Meeting.binary");Console.WriteLine(m2._name);Console.WriteLine(m2._location ?? "_location字段没有被序列化");Console.ReadKey();}//序列化static void SerializedWithBinaryFormatter(object obj, string fileName){//打开文件写成流Stream streamOut = File.OpenWrite(fileName);BinaryFormatter formatter = new BinaryFormatter();//把对象序列化到流中formatter.Serialize(streamOut, obj);//关闭流streamOut.Close();}//反序列化static object DeserializeWithBinaryFormatter(string fileName){//打开文件读成流Stream streamIn = File.OpenRead(fileName);BinaryFormatter formatter = new BinaryFormatter();object obj = formatter.Deserialize(streamIn);streamIn.Close();return obj;}
Meeting.binary文件在bin/debug文件夹中。
□ 使用ISerializable接口自定义序列化过程
如果想对序列化的过程有更多的控制,应该使用ISerializable接口,通过它的GetObjectData方法可以改变对象的字段值。
[Serializable]public class Location : ISerializable{public int x;public int y;public string name;public Location(int x, int y, string name){this.x = x;this.y = y;this.name = name;}protected Location(SerializationInfo info, StreamingContext context){x = info.GetInt32("i");y = info.GetInt32("j");name = info.GetString("k");}public void GetObjectData(SerializationInfo info, StreamingContext context){info.AddValue("i", x + 1);info.AddValue("j", y + 1);info.AddValue("k", name + "HELLO");}}
以上,不仅要实现接口方法GetObjectData,还需要提供对象的重载构造函数,从SerializationInfo实例中获取值。
在客户端:
Location loc1 = new Location(1,2,"qingdao");Location loc2;//序列化SerializedWithBinaryFormatter(loc1, "Location.binary");loc2 = (Location) DeserializeWithBinaryFormatter("Location.binary");Console.WriteLine(loc2.x);Console.WriteLine(loc2.y);Console.WriteLine(loc2.name);Console.ReadKey();
以上,使用BinaryFormatter类进行序列化和反序列化,存储的文件格式是二进制的,例如,打开Meeting.binary文件,我们看到:

有时候,我们希望文件的格式是xml。
□ 使用XmlSerializer类进行序列化和反序列化
XmlSerializer类进行序列化的存储文件是xml格式。用XmlSerializer类进行序列化的类不需要打上[Serializable]特性。
public class Car{[XmlAttribute(AttributeName = "model")]public string type;public string code;[XmlIgnore]public int age;[XmlElement(ElementName = "mileage")]public int miles;public Status status;public enum Status{[XmlEnum("使用中")]Normal,[XmlEnum("修复中")]NotUse,[XmlEnum("不再使用")]Deleted}}
在客户端:
//打开写进流Stream streamOut = File.OpenWrite("Car.xml");System.Xml.Serialization.XmlSerializer x = new XmlSerializer(car1.GetType());//序列化到流中x.Serialize(streamOut, car1);streamOut.Close();//打开读流Stream streamIn = File.OpenRead("Car.xml");//反序列化Car car2 = (Car) x.Deserialize(streamIn);Console.WriteLine(car2.type);Console.WriteLine(car2.code);Console.WriteLine(car2.miles);Console.WriteLine(car2.status);Console.ReadKey();
运行,打开bin/debug中的Car.xml文件如下:
<?xml version="1.0"?><Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" model="sedan"><code>001</code><mileage>1000</mileage><status>使用中</status></Car>
○ 类名Car成了xml的根节点
○ 打上[XmlAttribute(AttributeName = "model")]特性的字段变成了根节点的属性,AttributeName为属性别名
○ 枚举项可打上[XmlEnum("使用中")]特性
如果一个类中包含集合属性,比如以下的Department类包含一个类型List<Employee>的集合属性Employees。
public class Department{public Department(){Employees = new List<Employee>();}public string Name { get; set; }[XmlArray("Staff")]public List<Employee> Employees { get; set; }}public class Employee{public string Name { get; set; }public Employee(){}public Employee(string name){Name = name;}}
在客户端:
class Program{static void Main(string[] args){var department = new Department();department.Name = "销售部";department.Employees.Add(new Employee("张三"));department.Employees.Add(new Employee("李四"));XmlSerializer serializer = new XmlSerializer(department.GetType());//打开写进流Stream streamOut = File.OpenWrite("Department.xml");serializer.Serialize(streamOut, department);streamOut.Close();}}
查看bin/debug中的Department.xml文件。
<?xml version="1.0"?><Department xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Name>销售部</Name><Staff><Employee><Name>张三</Name></Employee><Employee><Name>李四</Name></Employee></Staff></Department>
总结:
1、使用BinaryFormatter类序列化到二进制文件
2、使用XmlSerializer类序列化到xml文件
3、使用ISerializable接口自定义序列化过程
初识序列化和反序列化,使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化的更多相关文章
- 谁能在同一文件序列化多个对象并随机读写(反序列化)?BinaryFormatter、SoapFormatter、XmlSerializer还是BinaryReader
谁能在同一文件序列化多个对象并随机读写(反序列化)?BinaryFormatter.SoapFormatter.XmlSerializer还是BinaryReader 随机反序列化器 +BIT祝威+悄 ...
- 数据结构-List接口-LinkedList类-Set接口-HashSet类-Collection总结
一.数据结构:4种--<需补充> 1.堆栈结构: 特点:LIFO(后进先出);栈的入口/出口都在顶端位置;压栈就是存元素/弹栈就是取元素; 代表类:Stack; 其 ...
- .NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结(XmlSerializer,XmlTypeAttribute,XmlElementAttribute,XmlAttributeAttribute,XmlArrayAttribute...)
序列化和反序列化是指什么? 序列化(seriallization): 将对象转化为便于传输的数据格式, 常见的序列化格式:二进制格式,字节数组,json字符串,xml字符串.反序列化(deserial ...
- ISerializable接口
继承ISerializable接口可以灵活控制序列化过程 格式化器的工作流程:格式化器再序列化一个对象的时候,发现对象继承了ISerializable接口,那它就会忽略掉类型所有的序列化特性,转而调用 ...
- typescript可索引接口 类类型接口
/* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据, ...
- java类生命周期,类的“加载,连接,初始化,使用,卸载过程”详解
“ 如果说核心类库的 API 比做数学公式的话,那么 Java 虚拟机的知识就好比公式的推导过程” 每本Java入门书籍在介绍Java这门语言的时候都会提到Java跨平台,“一次解释,到处运行的特点“ ...
- Typescript中的可索引接口 类类型接口
/* 5.typeScript中的接口 可索引接口 类类型接口 */ /* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用 ...
- Java基础进阶:多态与接口重点摘要,类和接口,接口特点,接口详解,多态详解,多态中的成员访问特点,多态的好处和弊端,多态的转型,多态存在的问题,附重难点,代码实现源码,课堂笔记,课后扩展及答案
多态与接口重点摘要 接口特点: 接口用interface修饰 interface 接口名{} 类实现接口用implements表示 class 类名 implements接口名{} 接口不能实例化,可 ...
- 序列化流与反序列化流,打印流,工具类commons-IO
1序列化流与反序列化流 用于从流中读取对象的操作流 ObjectInputStream 称为 反序列化流 用于向流中写入对象的操作流 ObjectOutputStream 称为 序列化流 特 ...
随机推荐
- git merge branch to master
git checkout master git pull git merge testbranch git push
- MYSQL数据库链接层- SUMMER-SQL
2015年3月31日 18:27:34 最后编辑: 2016年4月17日 00:22:00 星期日 最后编辑: 2018-4-25 16:58:44 星期三 最新代码: https://gitee.c ...
- centos memcached
2014年1月19日 16:58:37 memcached 是基于libevent事件监听功能的,所以要安装 libevent 和 libevent-devel 启动命令 ./memcached -d ...
- [转]hadoop新手错误解决方法
解决Exception: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z 等一系列问题,lja ...
- android:怎么实现一个控件与另一个指定控件左对齐
https://segmentfault.com/q/1010000003905460?_ea=425861 针对你这种情况,最简单的一种办法是,设置两个TextView的宽度为固定值,且相等. Li ...
- RocketMQ 部署
1. 下载 下载RocketMQwget https://github.com/alibaba/RocketMQ/releases/download/v3.2.6/alibaba-rocketmq-3 ...
- MVC中使用Web API和EntityFramework
在ASP.NET MVC中使用Web API和EntityFramework构建应用程序 最近做了一个项目技术预研:在ASP.NET MVC框架中使用Web API和EntityFramework ...
- 【AtCoder】ARC093
C - Traveling Plan 相当于一个环,每次删掉i点到两边的距离,加上新相邻的两个点的距离 代码 #include <bits/stdc++.h> #define fi fir ...
- docker容器重启故障
问题 强杀docker进程后,重启docker.docker中的容器无法启动并报错,报错内容如下 docker restart ae1f7b2c2f15 Error response from dae ...
- CSS------Filter属性的使用方法
转载: http://www.w3cplus.com/css3/ten-effects-with-css3-filter