深入浅出之C# 串行化与反串行化
来源:互联网 发布:工程造价数据 编辑:程序博客网 时间:2024/05/29 17:48
序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制。其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方。
串行化是指存储和获取磁盘文件、内存或其他地方中的对象。在串行化时,所有的实例数据都保存到存储介质上,
在取消串行化时,对象会被还原,且不能与其原实例区别开来。只需给类添加Serializable属性,就可以实现串行化实
的成员。反串行化是串行化的逆过程,数据从存储介质中读取出来,并赋给类的实例变量。串行化能保存现有对象的所有状态,
我想我们以前见过的一些游戏的角色账户中的dat文件应该就是被串行化的结果。我尝试了打开一个dat文件,果真得到了
我需要的信息,一些角色的等级之类的信息果然在里面。
在取消串行化时,对象会被还原,且不能与其原实例区别开来。只需给类添加Serializable属性,就可以实现串行化实
的成员。反串行化是串行化的逆过程,数据从存储介质中读取出来,并赋给类的实例变量。串行化能保存现有对象的所有状态,
我想我们以前见过的一些游戏的角色账户中的dat文件应该就是被串行化的结果。我尝试了打开一个dat文件,果真得到了
我需要的信息,一些角色的等级之类的信息果然在里面。
.NET框架提供了两种串行化的方式:
1、是使用BinaryFormatter进行串行化;
2、使用SoapFormatter进行串行化;
3、使用XmlSerializer进行串行化。
第一种方式提供了一个简单的二进制数据流以及某些附加的类型信息,而第二种将数据流格式化为XML存储;第三种其实和第二种差不多也是XML的格式存储,只不过比第二种的XML格式要简化很多(去掉了SOAP特有的额外信息)。
可以使用[Serializable]属性将类标志为可序列化的。如果某个类的元素不想被序列化,1、2可以使用[NonSerialized]属性来标志,2、可以使用[XmlIgnore]来标志。
1、使用BinaryFormatter进行串行化
下面是一个可串行化的类:
1. using System;
2.
3. using System.Data;
4.
5. using System.Configuration;
6.
7. using System.Web;
8.
9. using System.Web.Security;
10.
11. using System.Web.UI;
12.
13. using System.Web.UI.WebControls;
14.
15. using System.Web.UI.WebControls.WebParts;
16.
17. using System.Web.UI.HtmlControls;
18.
19. using System.IO;
20.
21. using System.Runtime.Serialization.Formatters.Binary;
22.
23. /**////
24.
25. 的摘要说明
26.
27. [Serializable]
28.
29. public class ClassToSerialize
30.
31. {
32.
33. public int id = 100;
34.
35. public string name = "Name";
36.
37. [NonSerialized]
38.
39. public string Sex = "男";
40.
41. }
42.
下面是串行化和反串行化的方法:
1. public void SerializeNow()
2.
3. {
4.
5. ClassToSerialize c = new ClassToSerialize();
6.
7. FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Create);
8.
9. BinaryFormatter b = new BinaryFormatter();
10.
11. b.Serialize(fileStream, c);
12.
13. fileStream.Close();
14.
15. }
16.
17. public void DeSerializeNow()
18.
19. {
20.
21. ClassToSerialize c = new ClassToSerialize();
22.
23. c.Sex = "kkkk";
24.
25. FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
26.
27. BinaryFormatter b = new BinaryFormatter();
28.
29. c = b.Deserialize(fileStream) as ClassToSerialize;
30.
31. Response.Write(c.name);
32.
33. Response.Write(c.Sex);
34.
35. fileStream.Close();
36.
37. }
38.
调用上述两个方法就可以看到串行化的结果:Sex属性因为被标志为[NonSerialized],故其值总是为null。
2、使用SoapFormatter进行串行化
和BinaryFormatter类似,我们只需要做一下简单修改即可:
a.将using语句中的.Formatter.Binary改为.Formatter.Soap;
b.将所有的BinaryFormatter替换为SoapFormatter.
c.确保报存文件的扩展名为.xml
经过上面简单改动,即可实现SoapFormatter的串行化,这时候产生的文件就是一个xml格式的文件。
3、使用XmlSerializer进行串行化
关于格式化器还有一个问题,假设我们需要XML,但是不想要SOAP特有的额外信息,那么我们应该怎么办呢?有两中方案:要么编写一个实现IFormatter接口的类,采用的方式类似于SoapFormatter类,但是没有你不需要的信息;要么使用库类XmlSerializer,这个类不使用Serializable属性,但是它提供了类似的功能。
如果我们不想使用主流的串行化机制,而想使用XmlSeralizer进行串行化我们需要做一下修改:
a.添加System.Xml.Serialization命名空间。
b.Serializable和NoSerialized属性将被忽略,而是使用XmlIgnore属性,它的行为与NoSerialized类似。
c.XmlSeralizer要求类有个默认的构造器,这个条件可能已经满足了。
下面看示例:
要序列化的类:
1. using System;
2.
3. using System.Data;
4.
5. using System.Configuration;
6.
7. using System.Web;
8.
9. using System.Web.Security;
10.
11. using System.Web.UI;
12.
13. using System.Web.UI.WebControls;
14.
15. using System.Web.UI.WebControls.WebParts;
16.
17. using System.Web.UI.HtmlControls;
18.
19. using System.Xml.Serialization;
20.
21. [Serializable]
22.
23. public class Person{
24.
25. private string name;
26.
27. public string Name{
28.
29. get{
30.
31. return name;
32.
33. }
34.
35. set{
36.
37. name = value;
38.
39. }}
40.
41. public string Sex;
42.
43. public int Age = 31;
44.
45. public Course[] Courses;
46.
47. public Person()
48.
49. {}
50.
51. public Person(string Name){
52.
53. name = Name;
54.
55. Sex = "男";
56.
57. }
58.
59. }
60.
61. [Serializable]
62.
63. public class Course{
64.
65. public string Name;
66.
67. [XmlIgnore]
68.
69. public string Description;
70.
71. public Course(){}
72.
73. public Course(string name, string description){
74.
75. Name = name;
76.
77. Description = description;
78.
79. }
80.
81. }
82.
序列化和反序列化方法:
1. public void XMLSerialize()
2.
3. {
4.
5. Person c = new Person("cyj");
6.
7. c.Courses = new Course[2];
8.
9. c.Courses[0] = new Course("英语", "交流工具");
10.
11. c.Courses[1] = new Course("数学","自然科学");
12.
13. XmlSerializer xs = new XmlSerializer(typeof(Person));
14.
15. Stream stream = new FileStream("c:\\cyj.XML",FileMode.Create,FileAccess.Write,FileShare.Read);
16.
17. xs.Serialize(stream,c);
18.
19. stream.Close();
20.
21. }
22.
23. public void XMLDeserialize()
24.
25. {
26.
27. XmlSerializer xs = new XmlSerializer(typeof(Person));
28.
29. Stream stream = new FileStream("C:\\cyj.XML",FileMode.Open,FileAccess.Read,FileShare.Read);
30.
31. Person p = xs.Deserialize(stream) as Person;
32.
33. Response.Write(p.Name);
34.
35. Response.Write(p.Age.ToString());
36.
37. Response.Write(p.Courses[0].Name);
38.
39. Response.Write(p.Courses[0].Description);
40.
41. Response.Write(p.Courses[1].Name);
42.
43. Response.Write(p.Courses[1].Description);
44.
45. stream.Close();
46.
47. }
48.
这里Course类的Description属性值将始终为null,生成的xml文档中也没有该节点,如下:
1. 男</Sex>
2.
3. 英语</Name>
4.
5. 交流工具
6.
7. 数学</Name>
8.
9. 自然科学
10.
、自定义序列化
如果你希望让用户对类进行串行化,但是对数据流的组织方式不完全满意,那么可以通过在自定义类中实现接口来自定义串行化行为。这个接口只有一个方法,GetObjectData.这个方法用于将对类对象进行串行化所需要的数据填进SerializationInfo对象。你使用的格式化器将构造SerializationInfo对象,然后在串行化时调用GetObjectData.如果类的父类也实现了ISerializable,那么应该调用GetObjectData的父类实现。
如果你实现了ISerializable,那么还必须提供一个具有特定原型的构造器,这个构造器的参数列表必须与GetObjectData相同。这个构造器应该被声明为私有的或受保护的,以防止粗心的开发人员直接使用它。
示例如下:
实现ISerializable的类:
1. using System;
2.
3. using System.Data;
4.
5. using System.Configuration;
6.
7. using System.Web;
8.
9. using System.Web.Security;
10.
11. using System.Web.UI;
12.
13. using System.Web.UI.WebControls;
14.
15. using System.Web.UI.WebControls.WebParts;
16.
17. using System.Web.UI.HtmlControls;
18.
19. using System.Runtime.Serialization;
20.
21. using System.Runtime.Serialization.Formatters.Binary;
22.
23. /**////
24.
25. 的摘要说明
26.
27. [Serializable]
28.
29. public class Employee:ISerializable
30.
31. {
32.
33. public int EmpId=100;
34.
35. public string EmpName="刘德华";
36.
37. [NonSerialized]
38.
39. public string NoSerialString = "NoSerialString-Test";
40.
41. public Employee()
42.
43. {
44.
45. //
46.
47. // TODO:在此处添加构造函数逻辑
48.
49. //
50.
51. }
52.
53. private Employee(SerializationInfo info, StreamingContext ctxt)
54.
55. {
56.
57. EmpId =(int)info.GetValue("EmployeeId", typeof(int));
58.
59. EmpName =(String)info.GetValue("EmployeeName",typeof(string));
60.
61. //NoSerialString =(String)info.GetValue("EmployeeString",typeof(string));
62.
63. }
64.
65. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
66.
67. {
68.
69. info.AddValue("EmployeeId", EmpId);
70.
71. info.AddValue("EmployeeName", EmpName);
72.
73. //info.AddValue("EmployeeString", NoSerialString);
74.
75. }
76.
77. }
78.
序列化和反序列化方法:
1. public void OtherEmployeeClassTest()
2.
3. {
4.
5. Employee mp = new Employee();
6.
7. mp.EmpId = 10;
8.
9. mp.EmpName = "邱枫";
10.
11. mp.NoSerialString = "你好呀";
12.
13. Stream steam = File.Open("c:\\temp3.dat", FileMode.Create);
14.
15. BinaryFormatter bf = new BinaryFormatter();
16.
17. Response.Write("Writing Employee Info:");
18.
19. bf.Serialize(steam,mp);
20.
21. steam.Close();
22.
23. mp = null;
24.
25. //反序列化
26.
27. Stream steam2 = File.Open("c:\\temp3.dat", FileMode.Open);
28.
29. BinaryFormatter bf2 = new BinaryFormatter();
30.
31. Response.Write("Reading Employee Info:");
32.
33. Employee mp2 =(Employee)bf2.Deserialize(steam2);
34.
35. steam2.Close();
36.
37. Response.Write(mp2.EmpId);
38.
39. Response.Write(mp2.EmpName);
40.
41. Response.Write(mp2.NoSerialString);
42.
43. }
44.
0 0
- 深入浅出之C# 串行化与反串行化
- C# 串行化与反串行化
- C# 串行化与反串行化
- C# 串行化与反串行化--自定义序列化
- C# 串行化与反串行化--自定义序列化
- C#基础知识回顾--串行化与反串行化
- PHP 之 串行化与反串行化
- C# 串行化与反串行化--使用BinaryFormatter进行串行化
- C# 串行化与反串行化--使用SoapFormatter进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)
- C# 串行化与反串行化--使用BinaryFormatter进行串行化
- C# 串行化与反串行化--使用SoapFormatter进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)
- PHP对象的串行化与反串行化
- 数组的串行化与反串行化
- PHP串行化与反串行化的调用
- vc2010 编译 qrencode
- SQL获取上个月第一天和最后一天的时间写法
- 在MVC4中采用UEditer(百度编辑器)脚本。报错,显示从客户端(<p>2323</p>)中检测到有潜在危险的Request.Form 值的处理办法
- 辛星浅析Linux中的进程基本知识以及fork的简单理解
- c# params
- 深入浅出之C# 串行化与反串行化
- wind7 安装Oracle客户端和pl/sql developer 无法成功连接数据库
- 微信支付之get_brand_wcpay_request:fail_invalid appid
- SSH框架面试题
- 服务器基础应用
- LeetCode114 Flatten Binary Tree to Linked List
- 利用DirectShow实现对视频文件H264编码与解码基类
- 富文本编辑器
- Android在WebView上构建Web应用程序