C# 的三种序列化方法

序列化是将一个对象转换成字节流以达到将其长期保存在内存、数据库或文件中的处理过程。它的主要目的是保存对象的状态以便以后需要的时候使用。与其相反的过程叫做反序列化。

  序列化一个对象

  为了序列化一个对象,我们需要一个被序列化的对象,一个容纳被序列化了的对象的(字节)流和一个格式化器。进行序列化之前我们先看看System.Runtime.Serialization名字空间。ISerializable接口允许我们使任何类成为可序列化的类。

  如果我们给自己写的类标识[Serializable]特性,我们就能将这些类序列化。除非类的成员标记了[NonSerializable],序列化会将类中的所有成员都序列化。

  序列化的类型
  • 二进制(流)序列化
  • SOAP序列化
  • XML序列化

  二进制(流)序列化:

  二进制(流)序列化是一种将数据写到输出流,以使它能够用来自动重构成相应对象的机制。二进制,其名字就暗示它的必要信息是保存在存储介质上,而这些必要信息要求创建一个对象的精确的二进制副本。在二进制(流)序列化中,整个对象的状态都被保存起来,而XML序列化只有部分数据被保存起来。为了使用序列化,我们需要引入System.Runtime.Serialization.Formatters.Binary名字空间. 下面的代码使用BinaryFormatter类序列化.NET中的string类型的对象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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类的对象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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对象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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里提供了两个格式化类:BinaryFormatterSoapFormatter,它们都继承了IFormatter接口。

  使用序列化

  序列化允许开发人员保存一个对象的状态并在需要的时候重构对象,同时很好地支持对象存储和数据交换。通过序列化,开发人员可以利用Web Service发送对象到远端应用程序,从一个域传输对象到另一个域,以XML的格式传输一个对象并能通过防火墙,或者在应用程序间保持安全性或用户特定信息等等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值