Three serialization methods in C#
序列化是将一个对象转换成字节流以达到将其长期保存在内存、数据库或文件中的处理过程。它的主要目的是保存对象的状态以便以后需要的时候使用。与其相反的过程叫做反序列化。
序列化一个对象
为了序列化一个对象,我们需要一个被序列化的对象,一个容纳被序列化了的对象的(字节)流和一个格式化器。进行序列化之前我们先看看System.Runtime.Serialization名字空间。ISerializable接口允许我们使任何类成为可序列化的类。
如果我们给自己写的类标识[Serializable]特性,我们就能将这些类序列化。除非类的成员标记了[NonSerializable],序列化会将类中的所有成员都序列化。
序列化的类型
二进制(流)序列化
SOAP序列化
XML序列化
二进制(流)序列化:
二进制(流)序列化是一种将数据写到输出流,以使它能够用来自动重构成相应对象的机制。二进制,其名字就暗示它的必要信息是保存在存储介质上,而这些必要信息要求创建一个对象的精确的二进制副本。在二进制(流)序列化中,整个对象的状态都被保存起来,而XML序列化只有部分数据被保存起来。为了使用序列化,我们需要引入System.Runtime.Serialization.Formatters.Binary名字空间. 下面的代码使用BinaryFormatter类序列化.NET中的string类型的对象。
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace SerializationTest { class Program { static void Main(string[] args) { //Serialization of String Object string strobj = "test string for serialization"; FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write , FileShare.None); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, strobj); stream.Close(); //Deserialization of String Object FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read , FileShare.Read ); string readdata = (string)formatter.Deserialize(readstream); readstream.Close(); Console.WriteLine(readdata); Console.ReadLine(); } } }
SOAP序列化:
SOAP协议是一个在异构的应用程序之间进行信息交互的理想的选择。我们需要在应用程序中添加System.Runtime.Serialization.Formatters.Soap名字空间以便在.Net中使用SOAP序列化。SOAP序列化的主要优势在于可移植性。SoapFormatter把对象序列化成SOAP消息或解析SOAP消息并重构被序列化的对象。下面的代码在.Net中使用SoapFormatter类序列化string类的对象。
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Soap ; namespace SerializationTest { class Program { static void Main(string[] args) { //Serialization of String Object string strobj = "test string for serialization"; FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write , FileShare.None); SoapFormatter formatter = new SoapFormatter(); formatter.Serialize(stream, strobj); stream.Close(); //Deserialization of String Object FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read , FileShare.Read ); string readdata = (string)formatter.Deserialize(readstream); readstream.Close(); Console.WriteLine(readdata); Console.ReadLine(); } } }
XML序列化:
根据MSDN的描述,“XML序列化将一个对象或参数的公开字段和属性以及方法的返回值转换(序列化)成遵循XSD文档标准的XML流。因为XML是一个开放的标准,XML能被任何需要的程序处理,而不管在什么平台下,因此XML序列化被用到带有公开的属性和字段的强类型类中,它的这些发生和字段被转换成序列化的格式(在这里是XML)存储或传输。”
我们必须添加System.XML.Serialization引用以使用XML序列化。使用XML序列化的基础是XmlSerializer。下面的代码是在.Net中使用XmlSerializer类序列化string对象。
using System; using System.IO; using System.Xml.Serialization; namespace SerializationTest { class Program { static void Main(string[] args) { //Serialization of String Object string strobj = "test string for serialization"; FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write , FileShare.None); XmlSerializer xmlserializer = new XmlSerializer(typeof(string)); xmlserializer.Serialize(stream, strobj); stream.Close(); //Deserialization of String Object FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read , FileShare.Read ); string readdata = (string)xmlserializer.Deserialize(readstream); readstream.Close(); Console.WriteLine(readdata); Console.ReadLine(); } } }
什么是格式化器?
一个格式化器用来确定一个对象的序列格式。它们目的是在网络上传输一个对象之前将其序列化成合适的格式。它们提供IFormatter接口。在.NET里提供了两个格式化类:BinaryFormatter和SoapFormatter,它们都继承了IFormatter接口。
使用序列化
Serialization allows developers to save the state of an object and reconstruct the object when needed, while well supporting object storage and data exchange. Through serialization, developers can use Web services to send objects to remote applications, transfer objects from one domain to another, transfer an object in XML format and pass a firewall, or maintain security or users between applications. specific information, etc.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to C# StringWriter. Here we discuss a brief overview on C# StringWriter Class and its working along with different Examples and Codes.

Guide to BinaryWriter in C#. Here we discuss syntax and explanation, how it works with examples to implement with proper codes.
