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()函数用于序列化一组表单元素,将表单内容编码为用于提交 ...
随机推荐
- 浅谈Java反射
什么是反射? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语 ...
- PTA_输入符号及符号个数打印沙漏(C++)
思路:想将所有沙漏所需符号数遍历一遍,然后根据输入的数判断需要输出多少多少层的沙漏,然后分两部分输出沙漏. #include<iostream> #include<cstring ...
- SpringBoot与日志框架2(日志内斗)
一.SpringBoot如何引入slf4j+logback框架的呢? 在POM文件中 <dependency> <groupId>org.springframework.boo ...
- vue笔记-列表渲染
用v-for把一个数组对应为一组元素 使用方法:v-for="(item,index) in items"//也可以使用of替代in { items:源数组 item:数组元素迭代 ...
- 巨坑npm run dev 报错 终于找到正确答案 Error: EPERM: operation not permitted, open '/data/public/build/css/add.p
Windows10环境 npm run dev 报错 终于找到正确答案 Error: EPERM: operation not permitted, open '/data/public/build ...
- 百度TTS的来由
#### https://home-assistant.io/components/tts.baidu/#### https://github.com/charleyzhu/HomeAssistant ...
- TypeScript 函数-Lambads 和 this 关键字的使用
let people = { name:["a","b","c","d"], /* getName:function() ...
- C#一句话判断两个List<T>是否相等
List1.All(List2.Contains) && List1.Count == List2.Count
- Git Log描述乱码问题解决方法
在git bash 中执行以下命令:git config --global core.quotepath off git config --global --unset i18n.logoutpute ...
- Address already in use: make_sock: could not bind to address 0.0.0.0:80
网上查了很多资料都是要杀进程,但是并没有找到占用的进程. 最后解决的方案是在apache配置文件下,去掉Listen 80 即可