asp.net中对象的序列化,方便网络传输
对象序列化 是将对象状态转换为可保持或传输的格式的过程。
反序列化 是将流转换为对象
序列化和反序列化相结合
可以使对象数据轻松的存储和传递
在 .NET 中,如果是对象可序列化,需要在 声明对象的开始部分加上 [Serializable]
这个属性,并且不能被继承
如一个类
[Serializable]
public class
A
{
public string title;
}
public class B :
A
{
public int total;
}
则 对象B 不可被序列化
在
.NET 提供了3种序列化的方式 BinaryFormatter, SoapFormatter和 XmlSerializer
,下面对这3种方式分别以代码形式做介绍
预备工作:
创建一个 实体对象
[Serializable]
public class
Entity
{
private int _total;
private string _title;
private double
_timeCount;
private int
_pageCount;
public Entity()
{
}
public int
Total
{
get
{ return _total; }
set { _total =
value; }
}
public string
Title
{
get
{ return _title; }
set { _title =
value; }
}
public double
TimeCount
{
get { return _timeCount; }
set {
_timeCount = value; }
}
public
int PageCount
{
get { return _pageCount;
}
set { _pageCount = value;
}
}
}
1. BinaryFormatter
说明: 需要引入命名空间
System.Runtime.Serialization 和
System.Runtime.Serialization.Formatters.Binary
//序列化对象
Entity entity = new
Entity();
entity.Total =
10;
entity.Title =
"测试";
entity.TimeCount =
0.18;
entity.PageCount =
5;
IFormatter formatter = new
BinaryFormatter();
Stream stream =
new FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile.txt",
FileMode.Create, FileAccess.Write,
FileShare.None);
formatter.Serialize(stream,
entity);
stream.Close();
//反序列化对象
IFormatter formatter1 =
new BinaryFormatter();
Stream
stream1 = new FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile.txt",
FileMode.Open, FileAccess.Read,
FileShare.Read);
Entity entity1 =
(Entity)formatter1.Deserialize(stream1);
stream1.Close();
Response.Write(entity1.Total + "
" + entity1.Title + "
" +
entity1.TimeCount + "
" + entity1.PageCount);
2. SoapFormatter
说明:
需要引入命名空间 System.Runtime.Serialization 和
System.Runtime.Serialization.Formatters.Soap
//序列化对象
Entity entity = new
Entity();
entity.Total =
10;
entity.Title =
"测试";
entity.TimeCount =
0.18;
entity.PageCount =
5;
IFormatter formatter = new
SoapFormatter();
Stream stream =
new FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile2.txt",
FileMode.Create, FileAccess.Write,
FileShare.None);
formatter.Serialize(stream,
entity);
stream.Close();
//反序列化对象
IFormatter formatter1 =
new SoapFormatter();
Stream
stream1 = new
FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile2.txt",
FileMode.Open, FileAccess.Read,
FileShare.Read);
Entity entity1 =
(Entity)formatter1.Deserialize(stream1);
stream1.Close();
Response.Write(entity1.Total + "
" + entity1.Title + "
" +
entity1.TimeCount + "
" + entity1.PageCount);
3. XmlSerializer
说明:
需要引入命名空间
System.Xml.Serialization
//序列化对象
Entity entity = new
Entity();
entity.Total =
10;
entity.Title =
"测试";
entity.TimeCount =
0.18;
entity.PageCount =
5;
XmlSerializer formatter =
new XmlSerializer(typeof(Entity));
Stream stream = new
FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile3.xml",
FileMode.Create, FileAccess.Write,
FileShare.None);
formatter.Serialize(stream,
entity);
stream.Close();
//反序列化对象
XmlSerializer formatter1
= new
XmlSerializer(typeof(Entity));
Stream stream1 = new
FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile3.xml",
FileMode.Open, FileAccess.Read,
FileShare.Read);
stream1.Seek(0,
SeekOrigin.Begin);
Entity
entity1 =
(Entity)formatter1.Deserialize(stream1);
stream1.Close();
Response.Write(entity1.Total + "
" + entity1.Title + "
" +
entity1.TimeCount + "
" + entity1.PageCount);
asp.net中对象的序列化,方便网络传输的更多相关文章
- Asp.net中Json的序列化和反序列化(一)
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- ASP.NET中JSON的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...
- ASP.NET 中JSON 的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- Asp.Net中JSON的序列化和反序列化-----JavaScriptSerializer ,加上自己工作心得
在工作中和手机通信用到web服务和javascriptSerializer,返回json数据,供手机端调用,一开始返回的数据是一大堆,比如 [{"word_picture9":&q ...
- ASP.NET中JSON的序列化和反序列化(转)
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- [转]ASP.NET中JSON的序列化和反序列化
本文转自:http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html JSON是专门为浏览器中的网页上运行的JavaScript代码 ...
- 为什么需要用到序列化?为什么HttpSession中对象要序列化
简单说就是为了保存在内存中的各种对象的状态,并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存Object States,但是Java给你提供一种应该比你自己好的保存对象状态的 ...
- 【原创】C++中对象的序列化
1.对象序列化 对象的序列化是指将对象的状态信息转换为可以存储或者传输的形式的过程.对象的反序列化是与序列化相反的过程. 在序列化期间,对象将其当前的状态写入到临时或者永久性的存储区,可以通过从存储区 ...
- java中对象的序列化和反序列化
[对象的序列化和反序列化 ] 1.定义:序列化--将对象写到一个输出流中.反序列化则是从一个输入流中读取一个对象.类中的成员必须是可序列化的,而且要实现Serializable接口,这样的类的对象才能 ...
随机推荐
- a链接中关于this的使用
a连接点击事件用 this 时,要用 onclick='click(this)',href='javascript:void()' a连接无法使用,要看看是不是自动变成ie7或者更低
- 【C语言】函数和自定义函数
函数,我之前也提到过一点点内容.其实函数是很好理解的,但是写起来又十分麻烦. 一. 函数引入 我们知道,C源程序是由函数组成的.请看下面的简单函数例子 #include <stdio.h ...
- hdu 1096 A+B for Input-Output Practice (VIII)
A+B for Input-Output Practice (VIII) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- JAXB - The Object Factory
Usually hidden in the middle of the list of the classes derived from the types defined in an XML sch ...
- scala学习笔记:理解lazy值
scala> var counter = 0 counter: Int = 0 scala> def foo = {counter += 1; counter} foo: Int scal ...
- 【HTTPS】Https和SSL学习笔记(一)
1. 什么是HTTPS 在说HTTPS之前必须要先说一下HTTP.我们平常浏览网页用的就是HTTP协议,HTTP协议之间传输的数据都是明文,这样对于一些敏感信息传输其实是不安全的,很容易被恶意窃取.应 ...
- ajaxError
$(document).ready(function () { $('input:button').click(function() { if($('#fileName').val() == '') ...
- 第27条:使用“class-continuation分类”隐藏实现细节
Objective-C动态消息系统(参见第11条)的工作方式决定了其不可能实现真正的私有方法或私有实例变量. 匿名分类的特点: 与普通的分类不同,它必须定义在其所接续的那个类的实现文件里. 唯一能声明 ...
- Objective-C 学习笔记(Day 2)
------------------------------------------- 如何根据题目准确完整清晰的声明一个类并实现给定的行为 /* //下面这个程序教大家如何根据题目去声明一个类,并 ...
- ios - 再细读KVO
[罗国强原创] KVO - Key-Value Observing. 它提供了一种机制,允许对象被通知到其他对象的具体特性的变化.它特别适用于一个应用的模型层与控制层的交互. 一种典型的应用场景是在一 ...