C#使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化
作者:Darren 发布时间:2023-01-16 02:11:54
序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程。对象一旦被序列化,就可以把对象状态保存到硬盘的某个位置,甚至还可以通过网络发送给另外一台机器上运行的进程。本篇主要包括:
使用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接口自定义序列化过程
来源:https://www.cnblogs.com/darrenji/p/4251611.html
猜你喜欢
- Spring提供的工具类,主要用于框架内部使用,这个类提供了一些简单的方法,并且提供了易于使用的方法在分割字符串,如CSV字符串,以及集合和
- 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax 时,在后台将数据封装为 JSON 字符串更是常见。之前在做项目的时候
- 概述冒泡排序是一种简单的排序算法。它重复地走访要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行
- 本文实例讲述了winform实现创建最前端窗体的方法。分享给大家供大家参考。具体实现方法如下:一、需求:1).需要这个窗体始终处于前端而且可
- 本文实例为大家分享了java实现酒店管理系统的具体代码,供大家参考,具体内容如下编写环境:MyEclipse2014+sql server2
- 什么是二叉堆二叉堆就是完全二叉树,或者是靠近完全二叉树结构的二叉树。在二叉树建树时采取前序建树就是建立的完全二叉树。也就是二叉堆。所以二叉堆
- 目录一、Gradle相比Maven的优势二、基本配置三、最佳实践四、总结一、Gradle相比Maven的优势配置简洁Maven是用pom.x
- 冒泡排序:就是按索引逐次比较相邻的两个元素,如果大于/小于(取决于需要升序排还是降序排),则置换,否则不做改变这样一轮下来,比较了n-1次,
- 概念里氏替换原则是任何基类出现的地方,子类一定可以替换它;是建立在基于抽象、多态、继承的基础复用的基石,该原则能够保证系统具有良好的拓展性,
- 好多时候,我们都需要知道某些目录下的文件什么时候被修改、删除过等,如果能用miniFilter驱动过滤来做的话当然是最好不过了,这是内核级别
- @PropertySource作用是:对自定义的properties文件加载使用:@PropertySource(value={"
- Java IO BufferedInputStream概要:BufferedInputStream是缓冲输入流,继承于Filte
- 前几天有个客户在系统上写了一段html语句,打开页面就显示一张炒鸡大的图片,影响美观。后来仔细想想,幸亏注入的仅仅是html语句,知道严重性
- Linux Hadoop 2.7.3 安装搭建Hadoop实现了一个分布式文件系统(Hadoop Distributed File Syst
- Java 切割字符串的几种方式//以data 为案例参数。String data = "2019-01-
- 本章,会对synchronized关键字进行介绍。涉及到的内容包括:1. synchronized原理2. synchronized基本规则
- 下面的示例提供对某个已存档类型的基本概述。示例// If compiling from the command line, compile
- 这节我们讨论了两种好玩的数据结构,栈和队列。老样子,什么是栈, 所谓的栈是栈(Stack)是操作限定在表的尾端进行的线性表。表尾由于要进行插
- 今天做统计时需要对X轴的地区按照地区代码(areaCode)进行排序,由于在构建XMLData使用的map来进行数据统计的,所以在统计过程中
- 目录一,功能二,工具三、效果图:四、数据库设计五、JAVA层次分析六、主要Java代码分析一,功能管理员登录图书借阅信息管理图书信息管理管理