1、为什么要虚拟化:将我们自己的对象状态(数据)持久化、传输等。
2、.net framwork 提供了两种序列化api
A、二进制序列化:
/// <summary> /// 二进制序列化方法 /// </summary> public static void Serializer() { MyObject obj = new MyObject(); obj.n1 = 1; obj.n2 = 24; obj.str = "Some String"; IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(@"F:\测试文件夹\ceshi.txt", FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, obj); stream.Flush(); stream.Close(); } /// <summary> /// 二进制反序列化 /// </summary> public static void DesSerializer() { IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(@"F:\测试文件夹\ceshi.txt", FileMode.Open, FileAccess.Read, FileShare.Read); MyObject obj = (MyObject)formatter.Deserialize(stream); stream.Close(); // Here‘s the proof. Console.WriteLine("n1: {0}", obj.n1); Console.WriteLine("n2: {0}", obj.n2); Console.WriteLine("str: {0}", obj.str); Console.ReadKey(); }
B、XML序列化
public class BaseInfo { List<Person> perList = new List<Person>(); [XmlElement(ElementName = "Person")] public List<Person> PerList { get { return perList; } set { perList = value; } } } public class Person { string name; int age; List<Books> bookList = new List<Books>(); /// <summary> /// 必须有默认的构造函数 /// </summary> public Person() { } public Person(string name, int age) { this.name = name; this.age = age; } public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } [XmlElement(ElementName = "Books")] public List<Books> BookList { get { return bookList; } set { bookList = value; } } } public class Books { List<Book> bookList = new List<Book>(); [XmlElement(ElementName = "Book")] public List<Book> BookList { get { return bookList; } set { bookList = value; } } } public class Book { string isbn; string title; public Book() { } public Book(string isbn, string title) { this.isbn = isbn; this.title = title; } public string ISBN { get { return isbn; } set { isbn = value; } } public string Title { get { return title; } set { title = value; } } }
/// <summary> /// xml序列化方法 /// </summary> public static void xmlSerialize() { Book b1 = new Book("111", "书1"); Book b2 = new Book("222", "书2"); Book b3 = new Book("333", "书3"); Books bs1 = new Books(); Books bs2 = new Books(); bs1.BookList.Add(b1); bs1.BookList.Add(b2); bs2.BookList.Add(b3); Person p1 = new Person("张三", 11); Person p2 = new Person("李四", 22); p1.BookList.Add(bs1); p2.BookList.Add(bs2); BaseInfo baseInfo = new BaseInfo(); baseInfo.PerList.Add(p1); baseInfo.PerList.Add(p2); StringWriter sw = new StringWriter(); //FileStream fs = new FileStream(@"F:\测试文件夹\baseInfo.xml",FileMode.CreateNew); //创建XML命名空间 XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer serializer = new XmlSerializer(typeof(BaseInfo)); serializer.Serialize(sw, baseInfo, ns); File.WriteAllText(@"F:\测试文件夹\baseInfo.xml", sw.ToString()); Console.Write(sw.ToString()); sw.Close(); Console.ReadKey(); } /// <summary> /// xml反序列化方法 /// </summary> public static void xmlDeserialize(string path) { //xml来源可能是外部文件,也可能是从其他系统获得 FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read); XmlSerializer xmlSearializer = new XmlSerializer(typeof(BaseInfo)); BaseInfo info = (BaseInfo)xmlSearializer.Deserialize(file); file.Close(); foreach (Person per in info.PerList) { Console.WriteLine("人员:"); Console.WriteLine(" 姓名:" + per.Name); Console.WriteLine(" 年龄:" + per.Age); foreach (Books b1 in per.BookList) { foreach (Book b in b1.BookList) { Console.WriteLine(" 书:"); Console.WriteLine(" ISBN:" + b.ISBN); Console.WriteLine(" 书名:" + b.Title); } } } Console.ReadKey(); }
时间: 2024-11-05 14:51:04