C#--序列化与反序列化

it2024-12-06  20

引用:

序列化与反序列化

解释:

序列化,就是将一个对象保存到文件中去,而反序列化就是将该文件重新保存为一个对象.

序列化的三种方式:

1.BinaryFormatter

class BinarySerialize : ISerialize { FileStream fs; string filepath = @"D:/1/1.txt"; public void Serialize(Book book) { using (fs = new FileStream(filepath, FileMode.Create)) { BinaryFormatter formate = new BinaryFormatter(); formate.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (fs = new FileStream(filepath, FileMode.Open)) { BinaryFormatter formate = new BinaryFormatter(); book = (Book)formate.Deserialize(fs); } return book; } } 序列化之后结果如下:

2.SoapFormatter

class SoapSerialize : ISerialize { FileStream fs; string filepath = @"D:/1/2.txt"; public void Serialize(Book book) { using (fs = new FileStream(filepath, FileMode.Create)) { SoapFormatter formate = new SoapFormatter(); formate.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (fs = new FileStream(filepath, FileMode.Open)) { SoapFormatter formate = new SoapFormatter(); book = (Book)formate.Deserialize(fs); } return book; } } 序列化之后,结果如下;

3.XmlSerializer

class XMLSeria : ISerialize { FileStream fs; string filepath = @"D:/1/3.txt"; public void Serialize(Book book) { using (fs = new FileStream(filepath, FileMode.Create)) { XmlSerializer formate = new XmlSerializer(typeof(Book)); formate.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (fs = new FileStream(filepath, FileMode.Open)) { XmlSerializer formate = new XmlSerializer(typeof(Book)); book = (Book)formate.Deserialize(fs); } return book; } } 序列化之后,结果如下: 完整源码如下: 代码段1: 定义类库Define namespace Define { [Serializable] public class Book { string bookname; public string BookName { get { return this.bookname; } set { this.bookname = value; } } int price; public int Price { get { return this.price; } set { this.price = value; } } public override string ToString() { return "BookName:" + this.bookname + "<br/>Price:" + this.price.ToString() + "<br/>"; } } public interface ISerialize { void Serialize(Book book); Book DeSerialize(); } class BinarySerialize : ISerialize { FileStream fs; string filepath = @"D:/1/1.txt"; public void Serialize(Book book) { using (fs = new FileStream(filepath, FileMode.Create)) { BinaryFormatter formate = new BinaryFormatter(); formate.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (fs = new FileStream(filepath, FileMode.Open)) { BinaryFormatter formate = new BinaryFormatter(); book = (Book)formate.Deserialize(fs); } return book; } } class SoapSerialize : ISerialize { FileStream fs; string filepath = @"D:/1/2.txt"; public void Serialize(Book book) { using (fs = new FileStream(filepath, FileMode.Create)) { SoapFormatter formate = new SoapFormatter(); formate.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (fs = new FileStream(filepath, FileMode.Open)) { SoapFormatter formate = new SoapFormatter(); book = (Book)formate.Deserialize(fs); } return book; } } class XMLSeria : ISerialize { FileStream fs; string filepath = @"D:/1/3.txt"; public void Serialize(Book book) { using (fs = new FileStream(filepath, FileMode.Create)) { XmlSerializer formate = new XmlSerializer(typeof(Book)); formate.Serialize(fs, book); } } public Book DeSerialize() { Book book; using (fs = new FileStream(filepath, FileMode.Open)) { XmlSerializer formate = new XmlSerializer(typeof(Book)); book = (Book)formate.Deserialize(fs); } return book; } } public interface BaseFacotry { ISerialize CreateSerialize(); } class BinaryFactory : BaseFacotry { public ISerialize CreateSerialize() { return new BinarySerialize(); } } class SoapFactory : BaseFacotry { public ISerialize CreateSerialize() { return new SoapSerialize(); } } class XMLFactory : BaseFacotry { public ISerialize CreateSerialize() { return new XMLSeria(); } } } 代码段2: 前台调用: <asp:Button ID="BinaryFactory" runat="server" Text="BinarySerialize" οnclick="Btn_Click" /> <asp:Button ID="SoapFactory" runat="server" Text="SoapSerialize" οnclick="Btn_Click" /> <asp:Button ID="XMLFactory" runat="server" Text="XMLSerialize" οnclick="Btn_Click" /> protected void Btn_Click(object sender, EventArgs e) { Book book = new Book(); book.BookName = "入门经典"; book.Price = 120; string id = ((Button)sender).ID; BaseFacotry factory = (BaseFacotry)Assembly.Load("Define").CreateInstance("Define." + id); ISerialize serialize = factory.CreateSerialize(); serialize.Serialize(book); Book b = serialize.DeSerialize(); Response.Write(b.ToString()); }

转载于:https://www.cnblogs.com/oneword/archive/2009/11/08/1598399.html

最新回复(0)