Reference:
Serialization and deserialization
Explain:
Serialization is to save an object to a file, and deserialization is to resave the file as an object.
Three ways to serialize:
1.BinaryFormatter
Class Binaryserialize:iserialize { FileStream fs; string filepath = @ "D:/1/1.txt"; public void Serialize { 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; } }
After serialization, the results are as follows:
2.SoapFormatter
Class Soapserialize:iserialize { FileStream fs; string filepath = @ "D:/1/2.txt"; public void Serialize { 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; } }
After serialization, the results are as follows;
3.XmlSerializer
Class Xmlseria:iserialize { FileStream fs; string filepath = @ "D:/1/3.txt"; public void Serialize { 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; } }
After serialization, the results are as follows:
The complete source code is as follows:
Code Snippet 1:
Defining the class library 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/>p Rice: "+ 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 Binaryforma Tter (); Book = (book) formate. Deserialize (FS); } return book; }} class Soapserialize:iserialize {FileStream fs; string filepath = @ "D:/1/2.txt"; public void Serialize {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 {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 Bina RyserialiZe (); }} class Soapfactory:basefacotry {public iserialize createserialize () {return new S Oapserialize (); }} class Xmlfactory:basefacotry {public iserialize createserialize () {return new XM Lseria (); } }}
Code Snippet 2:
Foreground call:
<asp:button id= "binaryfactory" runat= "Server" text= "binaryserialize" onclick= "Btn_click"/> <asp: Button id= "soapfactory" runat= "Server" text= "soapserialize" onclick= "Btn_click"/> <asp:button id= " Xmlfactory "runat=" Server "text=" xmlserialize "onclick=" Btn_click " /> protected void Btn_click (object sender, EventArgs e) {book Book = new book (); Book. BookName = "Getting Started classic"; Book. Price = +; 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 ()); }
Serialization and deserialization of c#--