usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Xml.Serialization;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.Runtime.Serialization.Formatters.Soap;namespaceserialization{classProgram {Static voidMain (string[] args) {Student Stu=NewStudent ("Zhang San", -,"Mountaineering"); ////binary Serialization //BinaryFormatter bf = new BinaryFormatter (); //FileStream fs = new FileStream (@ "D:\STU.SS", FileMode.Create); //BF. Serialize (FS, Stu); //FS. Close (); ////binary deserialization //BinaryFormatter bf = new BinaryFormatter (); //FileStream fs = new FileStream (@ "D:\STU.SS", FileMode.Open); //Student Student = (Student) bf. Deserialize (FS); //Student.method (); ///Serialization of/xml //XmlSerializer xml = new XmlSerializer (typeof (Student)); //FileStream fs = new FileStream (@ "D:\stu.xml", FileMode.Create); //XML. Serialize (FS, Stu); //FS. Close (); ////xml Deserialization //XmlSerializer xml = new XmlSerializer (typeof (Student)); //FileStream fs = new FileStream (@ "D:\stu.xml", FileMode.Open); //Student Student = (Student) XML. Deserialize (FS); //Student.method (); ///Serialization of/soap //SoapFormatter soap = new SoapFormatter (); //FileStream fs = new FileStream (@ "D:\stu.soap", FileMode.Create); //soap. Serialize (FS, Stu); //FS. Close (); //Soap DeserializationSoapFormatter soap =NewSoapFormatter (); FileStream FS=NewFileStream (@"D:\stu.soap", FileMode.Open); Student Student=(Student) soap. Deserialize (FS); Student.method (); }} [Serializable] Public classPerson { PublicPerson () {} PublicPerson (stringNameintAge ) { This. Name =name; This. Age =Age ; } Public stringName {Get; Set; } Public intAge {Get; Set; }} [Serializable] Public classStudent:person { PublicStudent () {} PublicStudent (stringNameintAgestringhobby):Base(name, age) { This. Hobby =Hobby; } Public stringHobby {Get; Set; } Public voidmethod () {Console.WriteLine ("Hello everyone, my name is {0}, this year {1} years old, I like {2}", This. Name, This. Age, This. Hobby); //console.readline (); } }}
View Code
C # Serialization (Binary, XML, Soap)