序列化与反序列化Serialize&Deserialize
序列化是指一个对象的实例可以被保存,保存成一个二进制串,当然,一旦被保存成二进制串,那么也可以保存成文本串了。
比如,一个计数器,数值为2,我们可以用字符串“2”表示。
如果有个对象,叫做connter,当前值为2,那么可以序列化成“2”,反向的,也可以从“2”得到值为2的计数器实例。
这样,关机时序列化它,开机时反序列化它,每次开机都是延续的。不会都是从头开始。
序列化概念的提出和实现,可以使我们的应用程序的设置信息保存和读取更加方便。
序列化有很多好处,比如,在一台机器上产生一个实例,初始化完毕,然后可以序列化,通过网络传送到另一台机器,然后反序列化,得到对象实例,之后再执行某些业务逻辑,得到结果,再序列化,返回第一台机器,第一台机器得到对象实例,得到结果。
一、二进制序列化与反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.IO; namespace Serialization
{
[Serializable]
public class A
{
public string Name { get; set; }
public string Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
var a=new A {Name = "mk", Id = ""};
var formater = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Stream stream =new FileStream("UserFileInfo.bin",FileMode.Create,FileAccess.Write,FileShare.None);
formater.Serialize(stream ,a);
stream.Close(); stream = new FileStream("UserFileInfo.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
var b=(A)formater.Deserialize(stream);
stream.Close();
Console.WriteLine("name{0}",b.Name);
Console.WriteLine("Id{0}",b.Id);
Console.Read();
} }
}
二、XML的序列化与反序列化
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization; namespace XMLSerialization
{
[Serializable]
public class A
{
public string Name { get; set; }
public string Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
var a = new A { Name = "yq", Id = "" };
//var formater = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
//Stream stream = new FileStream("UserFileInfo.bin", FileMode.Create, FileAccess.Write, FileShare.None);
XmlSerializer mySerializer=new XmlSerializer(typeof(A));
Stream stream = new FileStream("UserFileInfo.xml", FileMode.Create, FileAccess.Write, FileShare.None);
mySerializer.Serialize(stream, a);
stream.Close(); stream = new FileStream("UserFileInfo.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
var b = (A)mySerializer.Deserialize(stream);
stream.Close();
Console.WriteLine("name{0}", b.Name);
Console.WriteLine("Id{0}", b.Id);
Console.Read();
} }
}
UserFileInfo.xml返回内容
<?xml version="1.0"?>
<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>yq</Name>
<Id>121</Id>
</A>
序列化与反序列化Serialize&Deserialize的更多相关文章
- C#序列化与反序列化(Serialize,Deserialize)实例详解
这篇文章主要介绍了C#序列化与反序列化(Serialize,Deserialize)的方法,实例分析了C#序列化与反序列化的常见技巧,需要的朋友可以参考下 本文实例讲述了C#序列化与反序列化(Seri ...
- [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- PHP之:序列化和反序列化-serialize()和unserialize()
撰写日期:2016-7-7 10:56:40 参考PHP在线手册(php.net):http://php.net/manual/zh/function.serialize.php 1.序列化 seri ...
- 对象的序列化与反序列化-serialize与unserialize
1. 简介 对象的序列化的基本概念: 所谓对象的序列化,就是可以把某个对象的属性名称,属性值, 属性类型,类名 以字符串的形式保存到文件中,在你需要的时候可以重新恢复. 对象的反序列化的基本概念, 是 ...
- C# 序列化(Serialize)与反序列化(Deserialize)ZZ
序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制.其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方. .NET框架提供了两种种串行化的方式:1. ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- C# 序列化(Serialize)与反序列化(Deserialize)
序列化是将对象的状态信息转换为可保持或传输的格式的过程(一堆字符),比如转化为二进制.xml.json等的过程. 反序列化就是将在序列化过程中所生成的二进制串.xml.json等转换成数据结构或者对象 ...
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
- 7 Serialize and Deserialize Binary Tree 序列化及反序列化二叉树
原题网址:http://www.lintcode.com/zh-cn/problem/serialize-and-deserialize-binary-tree/# 设计一个算法,并编写代码来序列化和 ...
随机推荐
- Call to undefined function pg_
网上普遍的解决方案: 1.修改php.ini文件, 添加php_pgsql.dll扩展 2.如果是wamp这样类似的软件,可以直接通过图形化操作 这样操作后,大部分RD都是没有问的...但是为什么还提 ...
- 摄像头(5)使用Camera2 替代过时的Camera API
转自: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0428/2811.html 概要 从5.0开始(API Level 21 ...
- poj 1260 Pearls(dp)
题目:http://poj.org/problem?id=1260 题意:给出几类珍珠,以及它们的单价,要求用最少的钱就可以买到相同数量的,相同(或更高)质量的珍珠. 珍珠的替代必须是连续的,不能跳跃 ...
- poj3067
求交点的个数: 容易发现,对于两条航线(xi,yi)和(xj,yj),设xi<xj 只有yi>yj时两条航线存在交点: 于是我们考虑以x为第一关键字减序,y为第二关键字为减序排序: 则对于 ...
- spring+springMVC+JPA配置详解(使用缓存框架ehcache)
SpringMVC是越来越火,自己也弄一个Spring+SpringMVC+JPA的简单框架. 1.搭建环境. 1)下载Spring3.1.2的发布包:Hibernate4.1.7的发布包(没有使用h ...
- 【 D3.js 高级系列 — 2.0 】 捆图
捆图(Bundle)是 D3 中比较奇特的一个布局,只有两个函数,而且需要与其它布局配合使用.本文讲述捆图的制作方法. 有关捆图的例子极少,很容易找到的是:http://bl.ocks.org/mbo ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.3.6
If $A$ is a contraction, show that $$\bex A^*(I-AA^*)^{1/2}=(I-A^*A)^{1/2}A^*. \eex$$ Use this to sh ...
- Zabbix探索:Agent配置中Hostname错误引起的Agent.Ping报错
搭好了Zabbix_Server以后,添加了服务器本身和一台Windows的机器做测试,居然有这样的报警. Zabbix agent on zabbix_client is unreachable f ...
- MapReduce自定义二次排序流程
每一条记录开始是进入到map函数进行处理,处理完了之后立马就入自定义分区函数中对其进行分区,当所有输入数据经过map函数和分区函数处理完之后,就调用自定义二次排序函数对其进行排序. MapReduce ...
- IO_REMOVE_LOCK使用方法小结(转载加改正)
原文链接:http://www.programlife.net/io_remove_lock.html IO_REMOVE_LOCK(删除锁)的具体结构没有公开,WDK的文档中中查不到IO_REMOV ...