一.序列化又称为串行化,是.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的更多相关文章

  1. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  2. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. PHP之:序列化和反序列化-serialize()和unserialize()

    撰写日期:2016-7-7 10:56:40 参考PHP在线手册(php.net):http://php.net/manual/zh/function.serialize.php 1.序列化 seri ...

  4. PHP如何自动识别第三方Restful API的内容,自动渲染成 json、xml、html、serialize、csv、php等数据

    如题,PHP如何自动识别第三方Restful API的内容,自动渲染成 json.xml.html.serialize.csv.php等数据? 其实这也不难,因为Rest API也是基于http协议的 ...

  5. serialize存入数组

    原代码 def get_type type_list = "" if categories.include?"movie" type_list += " ...

  6. jquery中使用serialize() 序列化表单时 中文乱码问题

    序列化中文时之所以乱码是因为.serialize()调用了encodeURLComponent方法将数据编码了 解决方法就是进行解码 1 原因:.serialize()自动调用了encodeURICo ...

  7. 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 ...

  8. Leetcode: Serialize and Deserialize BST

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. jQuery.serialize() 函数详解////////////z

    serialize()函数用于序列化一组表单元素,将表单内容编码为用于提交的字符串. serialize()函数常用于将表单内容序列化,以便用于AJAX提交. 该函数主要根据用于提交的有效表单控件的n ...

  10. jquery.serialize

    jQuery - serialize() 方法 serialize() 方法通过序列化表单值,创建 URL 编码文本字符串. serialize()函数用于序列化一组表单元素,将表单内容编码为用于提交 ...

随机推荐

  1. SSH免密登录实现

    现在先想要把项目部署到linux系统中 通过使用maven添加tomcat插件可以做到,右击项目 配置这里的url,是部署到哪里的意思(比如我们现在将这个项目部署到以下系统的tomcat中) 此处只有 ...

  2. Shell编程-控制结构 | 基础篇

    if-then-else分支结构 if-then-else是一种基于条件测试结果的流程控制结构.如果测试结果为真,则执行控制结构中相应的命令列表:否则将进行另外一个条件测试或者退出该控制结构. if- ...

  3. centos7上PhantomJS 过期之后改用Chrome时填的坑

    突然有个自动化需求所以准备使用模拟点击的方法, 在使用之前的PhantomJS时,报错 UserWarning: Selenium support for PhantomJS has been dep ...

  4. v-charts修改点击图例事件,legendselectchanged

    html: <!--折线图--><ve-line :extend="item.chartExtend" :data-zoom="dataZoom&quo ...

  5. Handler Timer TimerTask ScheduledExecutor 循环任务解析

    使用Handler执行循环任务 private Handler handler = new Handler(); private int mDelayTime = 1000; private Runn ...

  6. php 按月创建日志

    public function log($log_string){ //$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."files". ...

  7. [转]Unity-移动设备可用的压缩解压缩源码

    原文:http://www.manew.com/thread-103250-1-1.html 最近在做客户端数据的分离,不希望对项目有什么影响,也不太想用AssetBundle,太麻烦,就在网上找了找 ...

  8. Java线程安全相关概

  9. lavarel5.2官方文档阅读——架构基础

    <目录> 1.请求的生命周期 2.应用的架构 3.服务提供者 4.服务容器 5.Facades外立面(从这节起,看中文版的:https://phphub.org/topics/1783) ...

  10. 我用linux系统的采坑记

    我的新Ubuntu18,也没安装什么,但是在使用过程中总是莫名其妙的卡死,真的很烦.有时候cpu使用率接近100%,有时候貌似是内存不够了,但是我明明是8GB,这些小问题搞得我很恼火.这样的机器真的不 ...