ASP.NET之:序列化
来源:互联网 发布:mac校园网客户端 编辑:程序博客网 时间:2024/05/21 10:40
1 什么是序列化?为什么需要序列化
有两个最重要的原因促使对序列化的使用:一个原因是将对象的状态保持在存储媒体中,以便可以在以后重新创建精确的副本;
另一个原因是通过值将对象从一个应用程序域发送到另一个应用程序域中。
序列化的是对象的状态
也就是对象数据成员的值和方法是没有关系的
跨应用程序域通信时,以及用WEB服务时要用到序列化
2 .1 序列化的方式
一:BinaryFormatter序列化
序列化简单点来理解就是把内存的东西写到硬盘中,当然也可以写到内存中(这个内容我会在后面写一个例子).而反序列化就是从硬盘中把信息读到内存中.就这么简单,呵呵,现在来看下面的例子吧!
在这篇文章中我将使用BinaryFormatter序列化类Person作为例子,希望大家能从例子中深刻体会什么是序列化.
/// <summary>
/// ClassToSerialize 的摘要说明
/// </summary>
[Serializable]
public class Person
{
public int id;
public string name;
//[NonSerialized]
public string Sex;
}
在类的上面增加了属性:Serializable.(如果不加这个属性,将抛出SerializationException异常).
通过这个属性将Book标志为可以序列化的.当然也有另一种方式使类Book可以序列化,那就是实行ISerializable接口了.在这里大家要注意了:Serializable属性是不能被继承的咯!!!
如果你不想序列化某个变量,该怎么处理呢?很简单,在其前面加上属性[NonSerialized] .比如我不想序列化
好了,现在就告诉大家怎么实现序列化:
我们使用namespace:
using System;
using System.IO;
using System.RunTime.Serialization.Formatters.Binary;
序列化:
public void SerializeBinaryNow()
{
Person c = new Person();
c.id = 1;
c.name = "zhangjunwei";
c.Sex = "male";
using (FileStream fileStream = new FileStream("E:\\proj\\File\\temp.dat", FileMode.Create))
{
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, c);
}
}
反序列化:
public void DeSerializeBinaryNow()
{
Person c = new Person();
using (FileStream fileStream = new FileStream("E:\\proj\\File\\temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read))
{
BinaryFormatter b = new BinaryFormatter();
c = b.Deserialize(fileStream) as Person;
Response.Write("id=" + c.id);
Response.Write("</br>");
Response.Write("name=" + c.name);
Response.Write("</br>");
Response.Write("sex=" + c.Sex);
}
}
注意一定不要忘了: using System.Runtime.Serialization.Formatters.Binary;
命名空间。
2.2SOAP 序列化 XML 序列化
SOAP:
SoapFormatter b = new SoapFormatter();
XML:
XmlSerializer xs = new XmlSerializer(typeof(Person));
3 序列化方便对象的传输及储存;
它能将对象转成如XML/Bit流;
它是Session(进程外/SqlServer模式),ViewState,WebService,Remoting等的基础。
收录四:
例如学生类
public class Student
{
public int id;
public string name;
public bool sex;
public DateTime birthday;
...
}
// 序列化为byte[]
MemoryStream fs = new MemoryStream();
byte[] tmp = null;
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, student);
tmp = fs.ToArray();
将tmp作为bianry数据存到数据库
// 反序列化直接生成Student类
MemoryStream fs = new MemoryStream();
Student student = null;
fs = new MemoryStream(tmp);
fs.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
student = formatter.Deserialize(fs);
- ASP.NET之:序列化
- asp.net 序列化
- ASP.NET序列化
- ASP.NET 序列化
- ASP.NET AJAX框架编程之JSON序列化剖析
- ASP.NET AJAX框架编程之JSON序列化剖析
- asp.net 序列化方法
- asp.net 对象序列化
- ASP.NET中的序列化
- asp.net Json序列化
- Asp.net Json序列化
- asp.net Json序列化
- Asp.net 序列化应用实例
- asp.net xml读取及序列化
- asp.net ajax 序列化出错
- "序列化" Serializable 理解 (asp.net C#)
- "序列化" Serializable 理解 (asp.net C#)
- 序列化Serializable(asp.net C#)
- Linux wc命令详解
- POJ 2411 Mondriaan's Dream 状态压缩(DP)
- 新概念英语 Lesson 15
- ASP.NET 基础(7)
- 浅谈学习计划
- ASP.NET之:序列化
- joj1538
- Error the user data image is used by another emulator. aborting
- 正则表达式技巧,快速度上手!
- 1.2.两个链表的合并
- 数据库连接查询类型(左连接、右连接、全连接、内连接和交叉连接)
- LANMP安装总结
- 分析PA日志程序
- flex tree 怎么在java后台组合成合适的数据,我使用的是flex spring hibernate