C # serialization

Source: Internet
Author: User

1. Serialization generally has 2 (XML and 2 binary), Simple object serialization

usingSystem;usingSystem.IO;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.Windows.Forms;usingSystem.Xml.Serialization;namespacetest{//for XmlSerializer serialization, the default is that you can serialize an object even if you do not use an attribute serializable, BinaryFormatter you must use the serializable tag.      Public Partial classForm1:form {//XmlSerializer is XML serializationXmlSerializer xs =NewXmlSerializer (typeof(Student)); //Binary SerializationBinaryFormatter B =NewBinaryFormatter (); Student Student=NewStudent () {Name ="Xiao Ming", age = the };  PublicForm1 () {InitializeComponent (); //Serialization of XML            using(Stream stream =NewFileStream ("D:\\student.xml", FileMode.Create, FileAccess.Write, FileShare.Read)) {xs.            Serialize (stream, student); }            //Deserialization of XML            using(FileStream fs =NewFileStream ("D:\\student.xml", FileMode.Open, FileAccess.Read)) {Student Student=(Student) xs.            Deserialize (FS); }            //Binary Sequence serialization            using(FileStream FileStream =NewFileStream ("D:\\student.dat", FileMode.Create)) {BinaryFormatter B=NewBinaryFormatter (); //serialization class to add [Serializable] attributeB.serialize (FileStream, student); }            //Binary sequence deserialization            using(FileStream FileStream =NewFileStream ("D:\\student.dat", FileMode.Open, FileAccess.Read)) {BinaryFormatter BF=NewBinaryFormatter (); Student=(Student) bf.            Deserialize (FileStream); }}}}[serializable] Public classstudent{ Public stringName {Get;Set; }  Public intAge {Get;Set; }}

2. Serialization of Complex objects

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Xml.Serialization;namespacetest{ Public Partial classForm1:form {//XmlSerializer is XML serializationXmlSerializer xs =NewXmlSerializer (typeof(teacherstudent)); //Binary SerializationBinaryFormatter B =NewBinaryFormatter (); Teacherstudent teacherstudent=Newteacherstudent (); Teacher Teacher=NewTeacher () {Name ="Miss Wang", age = $ }; Student Student=NewStudent () {Name ="Xiao Ming", age = the };  PublicForm1 () {InitializeComponent (); //Serialization of XML            using(Stream stream =NewFileStream ("D:\\student.xml", FileMode.Create, FileAccess.Write, FileShare.Read)) {Teacherstudent.teacher=teacher; Teacherstudent.student=student; Xs.            Serialize (stream, teacherstudent); }            //Deserialization of XML            using(FileStream fs =NewFileStream ("D:\\student.xml", FileMode.Open, FileAccess.Read)) {teacherstudent=NULL; Teacherstudent=(teacherstudent) xs.            Deserialize (FS); }            //Binary Sequence serialization            using(FileStream FileStream =NewFileStream ("D:\\student.dat", FileMode.Create)) {BinaryFormatter B=NewBinaryFormatter ();                B.serialize (FileStream, teacher);            B.serialize (FileStream, student); }            //Binary sequence deserialization            using(FileStream FileStream =NewFileStream ("D:\\student.dat", FileMode.Open, FileAccess.Read)) {Teacher=NULL; Student=NULL; BinaryFormatter BF=NewBinaryFormatter (); Teacher=(Teacher) bf.                Deserialize (FileStream); Student=(Student) bf.            Deserialize (FileStream); }}}}[serializable] Public classstudent{ Public stringName {Get;Set; }  Public intAge {Get;Set; }} [Serializable] Public classteacher{ Public stringName {Get;Set; }  Public intAge {Get;Set; }} [Serializable] Public classteacherstudent{ PublicTeacher Teacher {Get;Set; }  PublicStudent Student {Get;Set; }}

3. Control the data before and after serialization/deserialization

usingSystem;usingSystem.IO;usingSystem.Runtime.Serialization;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.Windows.Forms;namespacetest{ Public Partial classForm1:form {//XmlSerializer is XML serializationBinaryFormatter B =NewBinaryFormatter (); Student Student=NewStudent () {Name ="Xiao Ming", age = the };  PublicForm1 () {InitializeComponent (); //Binary Sequence serialization            using(FileStream FileStream =NewFileStream ("D:\\student.dat", FileMode.Create)) {BinaryFormatter B=NewBinaryFormatter ();            B.serialize (FileStream, student); }            //Binary sequence deserialization            using(FileStream FileStream =NewFileStream ("D:\\student.dat", FileMode.Open, FileAccess.Read)) {BinaryFormatter BF=NewBinaryFormatter (); Student=(Student) bf.            Deserialize (FileStream); }}}}[serializable] Public classstudent{ Public stringName {Get;Set; }  Public intAge {Get;Set; } [OnSerializing ()]Internal voidOnserializingmethod (StreamingContext context) {//The formatter calls this method before serialization begins. Console.WriteLine ("The onserializing formatter calls this method before the start of serialization"); } [OnSerialized ()]Internal voidOnserializedmethod (StreamingContext context) {//The formatter calls this method after serialization. Console.WriteLine ("The onserialized formatter calls this method after serialization"); } [OnDeserializing ()]Internal voidOndeserializingmethod (StreamingContext context) {//The formatter calls this method before the deserialization begins. Console.WriteLine ("The ondeserializing formatter calls this method before deserialization begins"); } [OnDeserialized ()]Internal voidOndeserializedmethod (StreamingContext context) {//The formatter calls this method before serialization begins. Console.WriteLine ("The ondeserialized formatter calls this method before the start of serialization"); }}

C # serialization

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.