C# 序列化(Binary、Xml、Soap)

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   sp   

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Xml.Serialization;using System.Runtime.Serialization.Formatters.Binary;using System.Runtime.Serialization.Formatters.Soap;namespace Serialization{    class Program    {        static void Main(string[] args)        {            Student stu = new Student("張三", 30, "登山");                        ////二進位序列化            //BinaryFormatter bf = new BinaryFormatter();            //FileStream fs = new FileStream(@"D:\stu.ss", FileMode.Create);            //bf.Serialize(fs, stu);            //fs.Close();            ////二進位還原序列化            //BinaryFormatter bf = new BinaryFormatter();            //FileStream fs = new FileStream(@"D:\stu.ss", FileMode.Open);            //Student student = (Student)bf.Deserialize(fs);            //student.method();            ////xml序列化            //XmlSerializer xml = new XmlSerializer(typeof(Student));            //FileStream fs = new FileStream(@"D:\stu.xml",FileMode.Create);            //xml.Serialize(fs, stu);            //fs.Close();            ////xml還原序列化            //XmlSerializer xml = new XmlSerializer(typeof(Student));            //FileStream fs = new FileStream(@"D:\stu.xml", FileMode.Open);            //Student student = (Student)xml.Deserialize(fs);            //student.method();            ////soap序列化            //SoapFormatter soap = new SoapFormatter();            //FileStream fs = new FileStream(@"D:\stu.soap", FileMode.Create);            //soap.Serialize(fs, stu);            //fs.Close();            //soap還原序列化            SoapFormatter soap = new SoapFormatter();            FileStream fs = new FileStream(@"D:\stu.soap", FileMode.Open);            Student student = (Student)soap.Deserialize(fs);            student.method();                    }    }    [Serializable]    public class Person    {        public Person()        {        }        public Person(string name, int age)        {            this.Name = name;            this.Age = age;        }        public string Name        {            get;            set;        }        public int Age        {            get;            set;        }    }    [Serializable]    public class Student : Person    {        public Student()        {        }        public Student(string name, int age, string hobby)            : base(name, age)        {            this.Hobby = hobby;        }        public string Hobby        {            get;            set;        }        public void method()        {            Console.WriteLine("大家好,我叫{0},今年{1}歲,我喜歡{2}", this.Name, this.Age, this.Hobby);            //Console.ReadLine();        }    }}
View Code

 

C# 序列化(Binary、Xml、Soap)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.