原文链接:
序列化和反序列化最主要的作用有:
1、在进程下次启动时读取上次保存的对象的信息 2、在不同的AppDomain或进程之间传递数据 3、在分布式应用系统中传递数据......在C#中常见的序列化的方法主要也有三个:BinaryFormatter、SoapFormatter、XML序列化本文就通过一个小例子主要说说这三种方法的具体使用和异同点这个例子就是使用三种不同的方式把一个Book对象进行序列化和反序列化,当然这个Book类首先是可以被序列化的。Book类
CODE:
using System;
using System.Collections;using System.Text;namespace SerializableTest{ [Serializable] public class Book { public Book() { alBookReader = new ArrayList(); } public string strBookName; [NonSerialized] public string strBookPwd; private string _bookID; public string BookID { get { return _bookID; } set { _bookID = value; } } public ArrayList alBookReader; private string _bookPrice; public void SetBookPrice(string price) { _bookPrice = price; } public void Write() { Console.WriteLine("Book ID:" + BookID); Console.WriteLine("Book Name:" + strBookName); Console.WriteLine("Book Password:" + strBookPwd); Console.WriteLine("Book Price:" + _bookPrice); Console.WriteLine("Book Reader:"); for (int i = 0; i < alBookReader.Count; i++) { Console.WriteLine(alBookReader[i]); } } }}
CODE:
Book book = new Book();
book.BookID = "1"; book.alBookReader.Add("gspring"); book.alBookReader.Add("永春"); book.strBookName = "C#强化"; book.strBookPwd = "*****"; book.SetBookPrice("50.00"); BinarySerialize serialize = new BinarySerialize(); serialize.Serialize(book);
CODE:
BinarySerialize serialize = new BinarySerialize();
Book book = serialize.DeSerialize(); book.Write();
CODE:
using System;
using System.Collections.Generic;using System.Text;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace SerializableTest{ public class BinarySerialize { string strFile = "c:""book.data"; public void Serialize(Book book) { using (FileStream fs = new FileStream(strFile, FileMode.Create)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (FileStream fs = new FileStream(strFile, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); book = (Book)formatter.Deserialize(fs); } return book; } }}
CODE:
using System;
using System.Collections.Generic;using System.Text;using System.IO;using System.Runtime.Serialization.Formatters.Soap;namespace SerializableTest{ public class SoapSerialize { string strFile = "c:""book.soap"; public void Serialize(Book book) { using (FileStream fs = new FileStream(strFile, FileMode.Create)) { SoapFormatter formatter = new SoapFormatter(); formatter.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (FileStream fs = new FileStream(strFile, FileMode.Open)) { SoapFormatter formatter = new SoapFormatter(); book = (Book)formatter.Deserialize(fs); } return book; } }}
CODE:
using System;
using System.Collections.Generic;using System.Text;using System.IO;using System.Xml.Serialization;namespace SerializableTest{ public class XmlSerialize { string strFile = "c:""book.xml"; public void Serialize(Book book) { using (FileStream fs = new FileStream(strFile, FileMode.Create)) { XmlSerializer formatter = new XmlSerializer(typeof(Book)); formatter.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (FileStream fs = new FileStream(strFile, FileMode.Open)) { XmlSerializer formatter = new XmlSerializer(typeof(Book)); book = (Book)formatter.Deserialize(fs); } return book; } }}
CODE:
<?xml version="1.0"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <strBookName>C#强化</strBookName> <strBookPwd>*****</strBookPwd> <alBookReader> <anyType xsi:type="xsd:string">gspring</anyType> <anyType xsi:type="xsd:string">永春</anyType> </alBookReader> <BookID>1</BookID></Book>
CODE:
Book book = new Book();
book.BookID = "1"; book.alBookReader.Add("gspring"); book.alBookReader.Add("永春"); book.strBookName = "C#强化"; book.strBookPwd = "*****"; book.SetBookPrice("50.00"); Book book2 = new Book(); book2.BookID = "2"; book2.alBookReader.Add("gspring"); book2.alBookReader.Add("永春"); book2.strBookName = ".NET强化"; book2.strBookPwd = "*****"; book2.SetBookPrice("40.00"); book.relationBook = book2; book2.relationBook = book; BinarySerialize serialize = new BinarySerialize(); serialize.Serialize(book);