如何将对象序列化为二进制、Soap、Xml
来源:互联网 发布:淘宝童萌会手办 编辑:程序博客网 时间:2024/05/17 03:48
要序列化的对象(Person)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FirstConsole{ [Serializable] public class Person { private string _name; public string Name { set { this._name = value; } get { return this._name; } } private int _age; public int Age { set { this._age = value; } get { return this._age; } } private string _address; public string Address { set { this._address = value; } get { return this._address; } } public Person(string _name, int _age, string _address) { this.Name = _name; this.Age = _age; this.Address = _address; } public Person() { this.Name = "Sheldon"; this.Age = -1; this.Address = "NONE"; } public void PrintInfo() { System.Console.WriteLine("姓名:" + this.Name + "\t年龄:" + this.Age + "\t地址:" + this.Address); } }}
通过BinaryFormatter可以将对象序列化为二进制文件以及反序列化为对象。BinaryFormatter所在命名空间为using System.Runtime.Serialization.Formatters.Binary;
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace FirstConsole{ class Program { static void Main(string[] args) { #region 二进制序列化 BinaryFormatter //序列化 Person Sheldon = new Person("Sheldon", 26, "重庆市铜梁区--Binary"); using (FileStream fs = new FileStream("Sheldon.bin", FileMode.OpenOrCreate)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, Sheldon); } //反序列化 using (FileStream fs = new FileStream("Sheldon.bin", FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); Person SheldonNew= (Person)bf.Deserialize(fs); SheldonNew.PrintInfo(); } #endregion } }}
使用SoapFormatter可以将对象序列化为Soap对象以及反序列化。SoapFormatter所在命名空间为using System.Runtime.Serialization.Formatters.Soap;使用时要先添加System.Runtime.Serialization.Formatters.Soap.dll程序集的引用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using System.Runtime.Serialization.Formatters.Soap;using System.Xml.Serialization;using System.Xml;namespace FirstConsole{ class Program { static void Main(string[] args) { #region SOAPFormatter Person Sheldon = new Person("Sheldon", 26, "重庆市铜梁区--Soap"); using (FileStream fs = new FileStream("Sheldon.soap", FileMode.OpenOrCreate)) { SoapFormatter bf = new SoapFormatter(); bf.Serialize(fs, Sheldon); } Console.WriteLine("Serialized in BinaryFormat"); using (FileStream fs = new FileStream("Sheldon.soap", FileMode.Open)) { SoapFormatter bf = new SoapFormatter(); Person SheldonNew = (Person)bf.Deserialize(fs); Sheldon.PrintInfo(); } #endregion } }}
使用XmlSerializer可以将对象序列化为Xml格式,改类位于using System.Xml.Serialization;使用时先添加System.xml.dll的引用。同时,被序列化的Person对象必须要有无参构造函数。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.IO;using System.Xml.Serialization;using System.Xml;namespace FirstConsole{ class Program { static void Main(string[] args) { #region XML Person Sheldon = new Person("Sheldon", 26, "重庆市沙坪坝区--Xml"); using (FileStream fs = new FileStream("Sheldon.xml", FileMode.OpenOrCreate)) { XmlSerializer xs = new XmlSerializer(typeof(Person)); xs.Serialize(fs, Sheldon); } using (FileStream fs = new FileStream("Sheldon.xml", FileMode.Open)) { XmlSerializer xs = new XmlSerializer(typeof(Person)); Person SheldonNew = (Person)xs.Deserialize(fs); SheldonNew.PrintInfo(); } #endregion } }}
PS:也可以借助XmlWriter将对象转化为Xml格式的字符串。使用XmlWriter时要引用命名空间using System.Xml;
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using System.Runtime.Serialization.Formatters.Soap;using System.Xml.Serialization;using System.Xml;namespace FirstConsole{ class Program { static void Main(string[] args) { #region 将对象序列化为String StringBuilder builder = new StringBuilder(); Person Sheldon = new Person("Sheldon", 26, "重庆市沙坪坝区--XmlString"); using (XmlWriter writer = XmlWriter.Create(builder)) { XmlSerializer xs = new XmlSerializer(typeof(Person)); xs.Serialize(writer, Sheldon); } Console.WriteLine(builder.ToString()); #endregion } }}
0 0
- 如何将对象序列化为二进制、Soap、Xml
- 将对象序列化为XML
- 将对象序列化为XML字符串
- 如何将数据序列化为XML格式
- JAXB将XML序列化为对象与将对象序列化为XML(日常记录)
- 将对象以二进制形式序列化为文本
- c# 对象序列化为XML
- 序列化对象为xml文件,将xml文件反序列化为对象
- 将AE对象序列化为二进制文件
- Jquery 将表单序列化为Json对象
- Jquery 将表单序列化为Json对象
- Jquery 将表单序列化为Json对象
- Jquery 将表单序列化为Json对象
- Jquery 将表单序列化为Json对象
- 将表单序列化为json对象
- 将表单序列化为json对象
- Jquery 将表单序列化为Json对象
- 将表单你序列化为json对象
- wordpress
- 【BZOJ】【P1046】【HAOI2007】【上升序列】【题解】【dp】
- 编译android 硬解码支持库 libstagefright
- asp.net-母版页设计
- Thinkphp3.2.2如何建立前后台公用的控制器
- 如何将对象序列化为二进制、Soap、Xml
- Oracle虚拟机VirtualBox安装成功后的注意事项
- Class.forName的作用以及为什么要用它【转】
- LeetCode:Trapping Rain Water
- 界面库对大dpi的支持
- android ActivityGroup实现Tab效果
- Away3D--------------关于太阳(光晕和上帝之光)的表现
- 经典的机器学习方面源代码库(数据挖掘、计算机视觉、模式识别、信息检索)
- 【Android】 天气预报与空气质量