c#序列化和反序列化的详细使用.docx

上传人:b****1 文档编号:1920847 上传时间:2023-05-02 格式:DOCX 页数:10 大小:21.65KB
下载 相关 举报
c#序列化和反序列化的详细使用.docx_第1页
第1页 / 共10页
c#序列化和反序列化的详细使用.docx_第2页
第2页 / 共10页
c#序列化和反序列化的详细使用.docx_第3页
第3页 / 共10页
c#序列化和反序列化的详细使用.docx_第4页
第4页 / 共10页
c#序列化和反序列化的详细使用.docx_第5页
第5页 / 共10页
c#序列化和反序列化的详细使用.docx_第6页
第6页 / 共10页
c#序列化和反序列化的详细使用.docx_第7页
第7页 / 共10页
c#序列化和反序列化的详细使用.docx_第8页
第8页 / 共10页
c#序列化和反序列化的详细使用.docx_第9页
第9页 / 共10页
c#序列化和反序列化的详细使用.docx_第10页
第10页 / 共10页
亲,该文档总共10页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

c#序列化和反序列化的详细使用.docx

《c#序列化和反序列化的详细使用.docx》由会员分享,可在线阅读,更多相关《c#序列化和反序列化的详细使用.docx(10页珍藏版)》请在冰点文库上搜索。

c#序列化和反序列化的详细使用.docx

c#序列化和反序列化的详细使用

序列化和反序列化C#[Serializable]与[Serializable()]

这两个是一样的

这个标签是类可以被序列化的特性,表示这个类可以被序列化。

什么叫序列化?

我们都知道对象是暂时保存在内存中的,不能用U盘考走了,有时为了使用介质转移对象,并且把对象的状态保持下来,就需要把对象保存下来,这个过程就叫做序列化,通俗点,就是把人的魂(对象)收伏成一个石子(可传输的介质)

什么叫反序列化?

就是再把介质中的东西还原成对象,把石子还原成人的过程。

在进行这些操作的时候都需要这个可以被序列化,要能被序列化,就得给类头加[Serializable]特性。

通常网络程序为了传输安全才这么做。

不知道回答清晰满意否?

下面进行验证

将Person的Name属性改成Private,然后查看生成的personInfo.text,其内容如下:

xmlversion="1.0"?

>

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns:

xsd="http:

//www.w3.org/2001/XMLSchema">

200719

man

22

可以看到Name属性并没有出现在该文件中,反序列化生成的对象中Name属性值为NULL。

以上对c#序列化和反序列化的三种方式进行了举例说明。

当然您也可以决定一个类中那些属性序列化或不序列化,可以通过使用NonSerialized属性标记成员变量来防止它们被序列化,具体内容请查阅相关资料。

序列化是指将对象实例的状态存储到存储媒体的过程。

在此过程中,先将对象的公共字段和私有字段以及类的名称(包括类所在的程序集)转换为字节流,然后再把字节流写入数据流。

在随后对对象进行反序列化时,将创建出与原对象完全相同的副本。

我们经常需要将对象的字段值保存到磁盘中,并在以后检索此数据。

尽管不使用序列化也能完成这项工作,但这种方法通常很繁琐而且容易出错,并且在需要跟踪对象的层次结构时,会变得越来越复杂。

可以想象一下编写包含大量对象的大型业务应用程序的情形,程序员不得不为每一个对象编写代码,以便将字段和属性保存至磁盘以及从磁盘还原这些字段和属性。

序列化提供了轻松实现这个目标的快捷方法。

.NET公共语言运行时(CLR)管理对象在内存中的分布,.NET框架则通过使用反射提供自动的序列化机制。

对象序列化后,类的名称、程序集以及类实例的所有数据成员均被写入存储媒体中。

对象通常用成员变量来存储对其他实例的引用。

类序列化后,序列化引擎将跟踪所有已序列化的引用对象,以确保同一对象不被序列化多次。

.NET框架所提供的序列化体系结构可以自动正确处理对象图表和循环引用。

对对象图表的唯一要求是,由正在进行序列化的对象所引用的所有对象都必须标记为Serializable(请参阅基本序列化)。

否则,当序列化程序试图序列化未标记的对象时将会出现异常。

当反序列化已序列化的类时,将重新创建该类,并自动还原所有数据成员的值。

在C#中常见的序列化的方法主要也有三个:

BinaryFormatter、SoapFormatter、XML序列化。

本文就通过一个小例子主要说说这三种方法的具体使用和异同点。

新建一个vs2008控制台工程SerializableTest,添加一个Person类,加上[Serializable]使其可以被序列化

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespaceSerializableTest

{

[Serializable]

publicclassPerson

{

publicstringSno{get;set;}

publicstringName{get;set;}

publicstringSex{get;set;}

publicintAge{get;set;}

publicstringDisplayInfo()

{

return"我的学号是:

"+Sno+"\n我的名字是:

"+Name+"\n我的性别为:

"+Sex+"\n我的年龄:

"+Age+"\n";

}

}

}

一、BinaryFormatter序列化方式

1、序列化:

新建一个Person对象me,然后将其序列化保存到文件personInfo.txt中]

varme=newPerson

{

Sno="200719",

Name="yuananyun",

Sex="man",

Age=22

};

//创建一个格式化程序的实例

IFormatterformatter=newBinaryFormatter();

//创建一个文件流

Streamstream=newFileStream("c:

/personInfo.txt",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);

formatter.Serialize(stream,me);

stream.Close();

执行以上代码将创建一个personInfo.txt文件,它包含了me对象的程序集信息、类名和字段信息。

2、反序列化:

从文件personInfo.txt中还原一个对象

//反序列化

Streamdestream=newFileStream("c:

/personInfo.txt",FileMode.Open,

FileAccess.Read,FileShare.Read);

varstillme=(Person)formatter.Deserialize(destream);

stream.Close();

整个程序如下:

usingSystem;

usingSystem.IO;

usingSystem.Runtime.Serialization;

usingSystem.Runtime.Serialization.Formatters.Binary;

namespaceSerializableTest

{

classProgram

{

staticvoidMain(string[]args)

{

//创建一个格式化程序的实例

IFormatterformatter=newBinaryFormatter();

Console.WriteLine("对象序列化开始……");

varme=newPerson

{

Sno="200719",

Name="yuananyun",

Sex="man",

Age=22

};

//创建一个文件流

Streamstream=newFileStream("c:

/personInfo.txt",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);

formatter.Serialize(stream,me);

stream.Close();

Console.WriteLine("序列化结束!

\n");

Console.WriteLine("反序列化开始……");

//反序列化

Streamdestream=newFileStream("c:

/personInfo.txt",FileMode.Open,

FileAccess.Read,FileShare.Read);

varstillme=(Person)formatter.Deserialize(destream);

stream.Close();

Console.WriteLine("反序列化结束,输出对象信息……");

Console.WriteLine(stillme.DisplayInfo());

Console.ReadKey();

}

}

}

运行结果如下:

注意:

反序列化还原对象时,并不会调用Person类的构造函数

二、SoapFormatter序列化方式

与BinaryFormatter序列化方式类似,只需要把IFormatterformatter=newBinaryFormatter()改成IFormatterformatter=newSoapFormatter(),并且引用程序集System.Runtime.Serialization.Formatters.Soap.dll(.net自带的)

usingSystem;

usingSystem.IO;

usingSystem.Runtime.Serialization;

usingSystem.Runtime.Serialization.Formatters.Soap;

namespaceSerializableTest

{

classProgram

{

staticvoidMain(string[]args)

{

//创建一个格式化程序的实例

IFormatterformatter=newSoapFormatter();

Console.WriteLine("对象序列化开始……");

varme=newPerson

{

Sno="200719",

Name="yuananyun",

Sex="man",

Age=22

};

//创建一个文件流

Streamstream=newFileStream("c:

/personInfo.txt",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);

formatter.Serialize(stream,me);

stream.Close();

Console.WriteLine("序列化结束!

\n");

Console.WriteLine("反序列化开始……");

//反序列化

Streamdestream=newFileStream("c:

/personInfo.txt",FileMode.Open,

FileAccess.Read,FileShare.Read);

varstillme=(Person)formatter.Deserialize(destream);

stream.Close();

Console.WriteLine("反序列化结束,输出对象信息……");

Console.WriteLine(stillme.DisplayInfo());

Console.ReadKey();

}

}

}

结果与第一种方式一样。

序列化之后的文件是Soap格式的文件(简单对象访问协议(SimpleObjectAccessProtocol,SOAP),是一种轻量的、简单的、基于XML的协议,它被设计成在WEB上交换结构化的和固化的信息。

SOAP可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。

它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。

SOAP使用基于XML的数据结构和超文本传输协议(HTTP)的组合定义了一个标准的方法来使用Internet上各种不同操作环境中的分布式对象。

),其内容如下:

Envelopexmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns:

xsd="http:

//www.w3.org/2001/XMLSchema"xmlns:

SOAP-ENC="http:

//schemas.xmlsoap.org/soap/encoding/"xmlns:

SOAP-ENV="http:

//schemas.xmlsoap.org/soap/envelope/"xmlns:

clr="SOAP-ENV:

encodingStyle="http:

//schemas.xmlsoap.org/soap/encoding/">

Body>

Personid="ref-1"xmlns:

a1="

<_x003C_Sno_x003E_k__BackingFieldid="ref-3">200719

<_x003C_Name_x003E_k__BackingFieldid="ref-4">yuananyun

<_x003C_Sex_x003E_k__BackingFieldid="ref-5">man

<_x003C_Age_x003E_k__BackingField>22

Person>

Body>

Envelope>

三、XML序列化方式

usingSystem;

usingSystem.IO;

usingSystem.Runtime.Serialization;

usingSystem.Xml.Serialization;

namespaceSerializableTest

{

classProgram

{

staticvoidMain(string[]args)

{

//创建一个格式化程序的实例

XmlSerializerformatter=newXmlSerializer(typeof(Person));

Console.WriteLine("对象序列化开始……");

varme=newPerson

{

Sno="200719",

Name="yuananyun",

Sex="man",

Age=22

};

//创建一个文件流

Streamstream=newFileStream("c:

/personInfo.txt",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);

formatter.Serialize(stream,me);

stream.Close();

Console.WriteLine("序列化结束!

\n");

Console.WriteLine("反序列化开始……");

//反序列化

Streamdestream=newFileStream("c:

/personInfo.txt",FileMode.Open,

FileAccess.Read,FileShare.Read);

varstillme=(Person)formatter.Deserialize(destream);

stream.Close();

Console.WriteLine("反序列化结束,输出对象信息……");

Console.WriteLine(stillme.DisplayInfo());

Console.ReadKey();

}

}

}

结果与上述相同,xml序列化之后的文件就是一般的一个xml文件,personInfo.txt内容如下:

xmlversion="1.0"?

>

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xmlns:

xsd="http:

//www.w3.org/2001/XMLSchema">

200719

yuananyun

man

22

注意:

采用xml序列化的方式只能保存public的字段和可读写的属性,对于private等类型的字段不能进行序列化

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2