首页 > 代码库 > c#序列化

c#序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;

namespace LetMeShow
{
class Program
{
static void Main(string[] args)
{

//
Console.WriteLine("let me test it....");
Person p = new Person();
p.way();

}
}

[Serializable]
public class Person
{

public string Sno { get; set; }

public string Name { get; set; }

public string Sex { get; set; }

public int Age { set; get; }

public string DisplayInfo()
{
return "我的学号是:" + Sno + "\n 我的名字是:" + Name+
"\n我的性别为:"+Sex+"\n我的年龄是:"+Age+"\n";

}

//使用binaryFormatter 方式进行序列化..
public void way()
{
var me = new Person
{
Sno = "200717",
Name = "yuanyuan",
Sex = "man",
Age = 22

};

//创建一个格式程序实例...
//对象空间的引用的呀..
//using System.Runtime.Serialization.Formatters.Binary;
//using System.Runtime.Serialization;
IFormatter formatter = new BinaryFormatter();
//创建一个文件流...
//相当与是写成了一个txt文件了...
Stream stream = new FileStream(@"E:\xx\序列化", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
formatter.Serialize(stream,me);
stream.Close();

//我在此表示,跳过此章节.....
}

public void returnList()
{
IFormatter formatter = new BinaryFormatter();
Stream destream = new FileStream("",FileMode.Open,FileAccess.Read,FileShare.Read);
var stillme = (Person)formatter.Deserialize(destream);
//这样就实现了我们的反序列化的东西....
destream.Close();
}


//方式二
//IFormatter formatter=new SoapFormatter(); 并引用程序集...
//还要引用dll
//system runtime.serialzition.Formatters.soap.dll..
public void way2()
{
IFormatter formatter = new SoapFormatter();
Console.WriteLine("对象序列化开始....");
var me = new Person
{
Sno = "2008",
Name = "yuanyuan",
Sex = "man",
Age = 12

};

//都要加入[Serializable]中...
//创建一个文件流
Stream s = new FileStream("",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);
formatter.Serialize(s,me);
s.Close();
Console.WriteLine("序列化结束.....\n");
Console.WriteLine("反序列化....");


//反序列化...
Stream st = new FileStream("",FileMode.Open,FileAccess.Read,FileShare.Read);
var stillme = (Person)formatter.Deserialize(st);
//这就就实现了我们的反序列化的东西的呀...
st.Close();
Console.WriteLine("good job..well done");


//序列话之后,序列化之后,soap 格式的文件(simple object access protocol)
//是一种轻量的,简单的,基于xml的协议;它被设计在web上交换结构化的和固定化的信息;
//soap 可以和现存的许多因特网协议和格式结合使用...
//包括httpt smtp
//它还支持消息系统到远程调用(RPC)等大量的应用程序....
//它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。


}


//方式三:使用xml 来进行序列化...
public void way3()
{
XmlSerializer formatter = new XmlSerializer(typeof(Person));
//对象的实例化开始...

var me = new Person()
{
Sno="2008123",
Name="yuanyuan",
Sex="man",
Age=123
};

//还是要创建一个文件流...
Stream s = new FileStream("",FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);
formatter.Serialize(s,me);
s.Close();
Console.WriteLine("序列化结束...");
Console.WriteLine("开始反序列化...");
//为毛要用jsp呢....
Stream de = new FileStream("",FileMode.Open,FileAccess.Read,FileShare.Read);
var xx = (Person)formatter.Deserialize(de);
de.Close();
//这样就实现了我么序列化的解析的啦....
//解析后就是一半点额xml格式
//注意一点:xml序列化的方式只能保存public的字段和可选读的字段;
//对于private等类型的字段不能进行序列化的哦...


}

}
}

c#序列化