網站首頁 編程語言 正文
C#使用BinaryFormatter類、ISerializable接口、XmlSerializer類進行序列化和反序列化_C#教程
作者:Darren?Ji ? 更新時間: 2022-11-04 編程語言序列化是將對象轉換成字節流的過程,反序列化是把字節流轉換成對象的過程。對象一旦被序列化,就可以把對象狀態保存到硬盤的某個位置,甚至還可以通過網絡發送給另外一臺機器上運行的進程。本篇主要包括:
使用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
相關推薦
- 2022-08-02 利用go語言實現Git?重命名遠程分支??_Golang
- 2023-03-29 Pytorch損失函數torch.nn.NLLLoss()的使用_python
- 2022-04-03 詳解iOS?實現一對多代理方案_IOS
- 2022-07-29 C++超詳細講解函數對象_C 語言
- 2023-10-14 SqlServer--get 和 post 請求 http接口
- 2022-09-25 springBoot自動裝配的原理
- 2022-08-27 教你使用Python的pygame模塊實現拼圖游戲_python
- 2022-07-03 C語言詳細講解const的用法_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支