C# Serialize
一.序列化又称为串行化,是.NET运行时环境用来支持用户自定义类型的机制,目的是以某种存储给对象持久化,或者是将这种对象传输到另一个地方,
二. .NET框架提供了两种序列化的方式 一种是使用BinaryFormatter,另一种是SoapFormatter进行序列化,不过还有一种繁琐的序列化方式(XmlSerializer);
三.可以使用[Serializable]属性将类标记为可序列化,如果不想让某个类序列化,可以使用[NonSerialized] 或者 [XmlIgnore] 两种方式.
四.下面是一个可序列化的类
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;//引入命名空间
/// <summary>
/// ClassToSerialize 的摘要说明
/// </summary>
[Serializable] //可序列化标志
public class ClassToSerialize
{
public int id = 100;
public string name = "Name";
[NonSerialized] //不可序列化标志
public string Sex = "男";
}
五.序列化和反序列化的方法
public void SerializeNow()
{
Class1ToSerialize Class1To = new Class1ToSerialize();
FileStream fileStream = new FileStream("D:\\temp.txt", FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream,Class1To); //序列化 参数:流 对象
fileStream.Close();
}
public void DeSerializeNow()
{
Class1ToSerialize c = new Class1ToSerialize();
c.sex = "kkkk";
FileStream fileStream = new FileStream("D:\\temp.txt", FileMode.Open);
BinaryFormatter b = new BinaryFormatter();
c = b.Deserialize(fileStream) as Class1ToSerialize; //反序列化 替换对象
Console.Write(c.sex);
Console.Write(c.name);
fileStream.Close();
}
六.使用SoapFormatter进行串行化()
这个和BinaryFormatter基本一样
我们只需要做一个简单的修改
1.项目引用using System.Runtime.Serialization.Formatters.Soap;
2.引用using System.Runtime.Serialization.Formatters.Soap;命名空间
3.将formatters.binary改成formatters.Soap;
4.将所有的binaryFormatter改成soapformatter
5.将流改为XML格式的文件
研究:不过微软已经对SoapFormatter放弃了 因为不支持泛型的序列化
关于SoapFormatter的序列化结果 有诸多的Soap特性,为了除掉Soap特性,我们要使用XmlSerializer 库类
七.使用XmlSerialize进行序列化(支持泛型的Xml序列化)
XmlSerializer进行序列化 我们需要做一下修改
a.添加System.Xml.Serialization;命名空间
b.Serializable和NoSerialized属性将会被忽略,而是使用了[XmlIgnore] 它和NoSerialized类似
c.XmlSerializer要求被序列化的类要有默认的构造器 这个条件可能默认满足!
[Serializable]
public class Person
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Sex;
public Hobby[] hobby = new Hobby[2];
[XmlIgnore]
public int age = 23;
/// <summary>
/// 构造函数
/// </summary>
public Person()
{
}
/// <summary>
/// 带参数的构造函数
/// </summary>
/// <param name="Name">姓名</param>
public Person(string Name)
{
name = Name;
Sex = "男";
}
}
//序列化方法
public class XmlSerializeVoid
{
/// <summary>
/// XmlSerialize序列化方法
/// </summary>
/// <param name="person">系列化对象</param>
public void XmlSerialize(Person person)
{
XmlSerializer xml = new XmlSerializer(typeof(Person));
FileStream stream = new FileStream("D:\\myxml.xml", FileMode.Create);
xml.Serialize(stream,person); //序列化
stream.Close();
}
/// <summary>
/// 反序列化方法
/// </summary>
/// <param name="person">序列化对象</param>
public Person XmlDeserialize()
{
XmlSerializer xml = new XmlSerializer(typeof(Person));
FileStream stream = new FileStream("D:\\myxml.xml", FileMode.Open);
Person person = xml.Deserialize(stream) as Person;
stream.Close();
return person;
}
}
Main方法
static void Main(string[] args)
{
Person person = new Person("张子浩");
person.hobby[0] = new Hobby() { hobbyName = "打代码"};
person.hobby[1] = new Hobby() { hobbyName = "听歌曲" };
XmlSerializeVoid xmlSerialize = new XmlSerializeVoid();
xmlSerialize.XmlSerialize(person) ;
Console.WriteLine("序列化成功:");
Person newperson = xmlSerialize.XmlDeserialize();
Console.WriteLine("反序列化成功:");
Console.WriteLine("年龄"+newperson.age);
Console.WriteLine("姓名"+newperson.Name);
Console.WriteLine("性别"+newperson.Sex);
Console.WriteLine("爱好1"+newperson.hobby[0].hobbyName);
Console.WriteLine("爱好2" + newperson.hobby[1].hobbyName);
}


C# Serialize的更多相关文章
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] 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 ...
- PHP如何自动识别第三方Restful API的内容,自动渲染成 json、xml、html、serialize、csv、php等数据
如题,PHP如何自动识别第三方Restful API的内容,自动渲染成 json.xml.html.serialize.csv.php等数据? 其实这也不难,因为Rest API也是基于http协议的 ...
- serialize存入数组
原代码 def get_type type_list = "" if categories.include?"movie" type_list += " ...
- jquery中使用serialize() 序列化表单时 中文乱码问题
序列化中文时之所以乱码是因为.serialize()调用了encodeURLComponent方法将数据编码了 解决方法就是进行解码 1 原因:.serialize()自动调用了encodeURICo ...
- U家面试prepare: Serialize and Deserialize Tree With Uncertain Children Nodes
Like Leetcode 297, Serialize and Deserialize Binary Tree, the only difference, this is not a binary ...
- Leetcode: Serialize and Deserialize BST
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- jQuery.serialize() 函数详解////////////z
serialize()函数用于序列化一组表单元素,将表单内容编码为用于提交的字符串. serialize()函数常用于将表单内容序列化,以便用于AJAX提交. 该函数主要根据用于提交的有效表单控件的n ...
- jquery.serialize
jQuery - serialize() 方法 serialize() 方法通过序列化表单值,创建 URL 编码文本字符串. serialize()函数用于序列化一组表单元素,将表单内容编码为用于提交 ...
随机推荐
- RabbitMQ中RPC的实现及其通信机制
RabbitMQ中RPC的实现:客户端发送请求消息,服务端回复响应消息,为了接受响应response,客户端需要发送一个回调队列的地址来接受响应,每条消息在发送的时候会带上一个唯一的correlati ...
- easyui拓展验证结束日期大于等于开始日期
<div style="margin:20px 0px 20px 70px"> <span>有效期起始时间:</span><input i ...
- MongoDB与Spring整合(支持事务)——SpringDataMongoDB
1.将MongoDB设置为复制集模式 a.修改 mongod.cfg 文件,添加replSetName复制集名称 #replication: replication: replSetName: &qu ...
- 组合 数论 莫比乌斯反演 hdu1695
题解:https://blog.csdn.net/lixuepeng_001/article/details/50577932 题意:给定范围1-b和1-d求(i,j)=k的数对的数量 #includ ...
- nginx.conf 中php-ftp配置
location ~ .php$ { root /home/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_par ...
- OI暑假集训游记
莞中OI集训游记 Written BY Jum Leon. I 又是一载夏,本蒟蒻以特长生考入莞中,怀着忐忑的心情到了8月,是集训之际.怀着对算法学习的向往心情被大佬暴虐的一丝恐惧来到了 ...
- Microsoft Graph: Developer Blog
https://developer.microsoft.com/en-us/graph/blogs/announcing-30-days-of-microsoft-graph-blog-series/ ...
- TP框架下载功能
namespace Home\Controller; use Think\Controller; use Org\Net\Http; class IndexController extends Con ...
- 20175324 mycp
具体描述: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容 ...
- Educational Codeforces Round 8
开始填坑_(:з」∠)_ 628A - Tennis Tournament 20171124 小学数学题,\((x,y)=((n-1)\cdot(2b+1),np)\) #include< ...