C# 序列化与反序列化
using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Soap; using System.Xml.Serialization; using System.Runtime.Serialization; public class SerializeAndDeserialize { //1、使用BinaryFormatter进行串行化 public void SerializeNow() { ClassToSerialize c = new ClassToSerialize(); FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Create); BinaryFormatter b = new BinaryFormatter(); b.Serialize(fileStream, c); fileStream.Close(); } public void DeSerializeNow() { ClassToSerialize c = new ClassToSerialize(); c.Sex = "kkkk"; FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read); BinaryFormatter b = new BinaryFormatter(); c = b.Deserialize(fileStream) as ClassToSerialize; Console.WriteLine("Output :" + c.name); Console.WriteLine("Output :" + c.id); Console.WriteLine("Output :" + c.Sex); fileStream.Close(); } // 2、使用SoapFormatter进行串行化 public void SerializeNow_Soap() { ClassToSerialize c = new ClassToSerialize(); FileStream fileStream = new FileStream("c:\\temp2.dat", FileMode.Create); SoapFormatter s = new SoapFormatter(); s.Serialize(fileStream, c); fileStream.Close(); } public void DeSerializeNow_Soap() { ClassToSerialize c = new ClassToSerialize(); c.Sex = "kkkk"; FileStream fileStream = new FileStream("c:\\temp2.dat", FileMode.Open, FileAccess.Read, FileShare.Read); SoapFormatter s = new SoapFormatter(); c = s.Deserialize(fileStream) as ClassToSerialize; Console.WriteLine("Output :" + c.name); Console.WriteLine("Output :" + c.id); Console.WriteLine("Output :" + c.Sex); fileStream.Close(); } //3、使用XmlSerializer进行串行化 public void XMLSerialize() { Person c = new Person("cyj"); c.Courses = new Course[2]; c.Courses[0] = new Course("英语", "交流工具"); c.Courses[1] = new Course("数学", "自然科学"); XmlSerializer xs = new XmlSerializer(typeof(Person)); Stream stream = new FileStream("c:\\cyj.HTML", FileMode.Create, FileAccess.Write, FileShare.Read); xs.Serialize(stream, c); stream.Close(); } public void XMLDeserialize() { XmlSerializer xs = new XmlSerializer(typeof(Person)); Stream stream = new FileStream("C:\\cyj.XML", FileMode.Open, FileAccess.Read, FileShare.Read); Person p = xs.Deserialize(stream) as Person; Console.WriteLine("Output :" + p.Name); Console.WriteLine("Output :" + p.Age.ToString()); Console.WriteLine("Output :" + p.Courses[0].Name); Console.WriteLine("Output :" + p.Courses[0].Description); Console.WriteLine("Output :" + p.Courses[1].Name); Console.WriteLine("Output :" + p.Courses[1].Description); stream.Close(); } //4、自定义序列化 public void OtherEmployeeClassTest() { Employee mp = new Employee(); mp.EmpId = 10; mp.EmpName = "邱枫"; mp.NoSerialString = "你好呀"; Stream steam = File.Open("c:\\temp3.dat", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); Console.WriteLine("Output :" + "Writing Employee Info:"); bf.Serialize(steam, mp); steam.Close(); mp = null; //反序列化 Stream steam2 = File.Open("c:\\temp3.dat", FileMode.Open); BinaryFormatter bf2 = new BinaryFormatter(); Console.WriteLine("Output :" + "Reading Employee Info:"); Employee mp2 = (Employee)bf2.Deserialize(steam2); steam2.Close(); Console.WriteLine("Output :" + mp2.EmpId); Console.WriteLine("Output :" + mp2.EmpName); Console.WriteLine("Output :" + mp2.NoSerialString); } } [Serializable] public class ClassToSerialize { public int id = 100; public string name = "Name"; [NonSerialized] public string Sex = "男"; } [Serializable] public class Person { private string name; public string Name { get { return name; } set { name = value; } } public string Sex; public int Age = 31; public Course[] Courses; public Person() { } public Person(string Name) { name = Name; Sex = "男"; } } [Serializable] public class Course { public string Name; [XmlIgnore] public string Description; public Course() { } public Course(string name, string description) { Name = name; Description = description; } } [Serializable] public class Employee : ISerializable { public int EmpId = 100; public string EmpName = "work hard work smart work happy"; [NonSerialized] public string NoSerialString = "NoSerialString-Test"; public Employee() { } private Employee(SerializationInfo info, StreamingContext ctxt) { EmpId = (int)info.GetValue("EmployeeId", typeof(int)); EmpName = (String)info.GetValue("EmployeeName", typeof(string)); } public void GetObjectData(SerializationInfo info, StreamingContext ctxt) { info.AddValue("EmployeeId", EmpId); info.AddValue("EmployeeName", EmpName); } }
C# 序列号与反序列化总结
1、 C# 序列化与反序列化(此文)
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!