以下代码VS2008下编译通过。
/*
* 序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用。
* 序列化和反序列化最主要的作用有:
* 1、在进程下次启动时读取上次保存的对象的信息
* 2、在不同的AppDomain或进程之间传递数据
* 3、在分布式应用系统中传递数据
*
*/
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
using System.Xml;
namespace Serializable用法示例
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string filepath = Application.StartupPath + "\\test.dat";
public string filepath2 = Application.StartupPath + "\\test.xml";
private void Form1_Load(object sender, EventArgs e)
{
SerData4();
//string str= SerData5();
//DeSerData5(str);
}
private void button1_Click(object sender, EventArgs e)
{
DeSerData4();
}
/// <summary>
/// BinaryFormatter序列化与反序列化2
/// </summary>
private void SerData2()
{
Person person = new Person();
person.age = 18;
person.name = "Tom";
person.secret = "hello world";
FileStream fs = new FileStream(filepath, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, person);
fs.Close();
Console.WriteLine("------serialize2 end");
}
private void DeSerData2()
{
Person person = new Person();
FileStream fs = new FileStream(filepath, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
//反序列化得到的是一个Object对象,必须做下类型转化
person = (Person)bf.Deserialize(fs);
fs.Close();
//结果为18,Tom,因为secret没有被实例化
Console.WriteLine("------" + person.age + "," + person.name + "," + person.secret);
}
/// <summary>
/// SoapFormatter序列化与反序列化
/// </summary>
private void SerData3()
{
Person person = new Person();
person.age = 18;
person.name = "Tom";
person.secret = "hello world";
FileStream fs = new FileStream(filepath2, FileMode.Create);
SoapFormatter sf = new SoapFormatter();
sf.Serialize(fs, person);
fs.Close();
Console.WriteLine("------serialize3 end");
}
private void DeSerData3()
{
Person person = new Person();
FileStream fs = new FileStream(filepath2, FileMode.Open);
SoapFormatter sf = new SoapFormatter();
//反序列化得到的是一个Object对象,必须做下类型转化
person = (Person)sf.Deserialize(fs);
fs.Close();
//结果为18,Tom,因为secret没有被实例化
Console.WriteLine("------" + person.age + "," + person.name + "," + person.secret);
}
/// <summary>
/// XmlSerializer序列化与反序列化,
/// 也保存为xml文件,无额外信息。只能保存public类型的字段,
/// 而其它两种方法保存所有类型的字段
/// </summary>
private void SerData4()
{
Person person = new Person();
person.age = 18;
person.name = "Tom";
person.secret = "hello world";
FileStream fs = new FileStream(filepath2, FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(Person));
xs.Serialize(fs, person);
fs.Close();
Console.WriteLine("------serialize4 end");
}
private void DeSerData4()
{
Person person = new Person();
FileStream fs = new FileStream(filepath2, FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(Person));
//反序列化得到的是一个Object对象,必须做下类型转化
person = (Person)xs.Deserialize(fs);
fs.Close();
//结果为18,Tom,因为secret没有被实例化
Console.WriteLine("------" + person.age + "," + person.name + "," + person.secret);
}
/// <summary>
/// 利用XmlTextWriter将Person类与string相互转换
/// </summary>
private string SerData5()
{
Person person = new Person();
person.age = 18;
person.name = "Tom";
person.secret = "hello world";
MemoryStream ms = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(ms, System.Text.Encoding.UTF8);
xmlWriter.Indentation = 4;
xmlWriter.Formatting = Formatting.Indented;
XmlSerializer formatter = new XmlSerializer(typeof(Person));
formatter.Serialize(xmlWriter, person);
xmlWriter.Close();
string xmlstring = System.Text.Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine("{0},{1}",xmlstring.Length,xmlstring);
return xmlstring;
}
private Person DeSerData5(string xmlstring)
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(xmlstring);
MemoryStream ms = new MemoryStream(buffer);
XmlTextReader xmlReader = new XmlTextReader(ms);
XmlSerializer formatter = new XmlSerializer(typeof(Person));
Person person = (Person)formatter.Deserialize(xmlReader);
xmlReader.Close();
Console.WriteLine("------" + person.age + "," + person.name + "," + person.secret);
return person;
}
}
}
test.xml为:
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<age>18</age>
<name>Tom</name>
<secret>hello world</secret>
</Person>