Serialization and deserialization of c#--

Source: Internet
Author: User

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#--

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.