如何将对象序列化为二进制、Soap、Xml

来源:互联网 发布:淘宝童萌会手办 编辑:程序博客网 时间:2024/05/17 03:48

要序列化的对象(Person)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FirstConsole{    [Serializable]    public class Person    {        private string _name;        public string Name        {            set            {                this._name = value;            }            get            {                return this._name;            }        }        private int _age;        public int Age        {            set            {                this._age = value;            }            get            {                return this._age;            }        }        private string _address;        public string Address        {            set            {                this._address = value;            }            get            {                return this._address;            }        }        public Person(string _name, int _age, string _address)        {            this.Name = _name;            this.Age = _age;            this.Address = _address;        }        public Person()        {            this.Name = "Sheldon";            this.Age = -1;            this.Address = "NONE";        }        public void PrintInfo()        {            System.Console.WriteLine("姓名:" + this.Name + "\t年龄:" + this.Age + "\t地址:" + this.Address);        }    }}


 

通过BinaryFormatter可以将对象序列化为二进制文件以及反序列化为对象。BinaryFormatter所在命名空间为using System.Runtime.Serialization.Formatters.Binary;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace FirstConsole{    class Program    {        static void Main(string[] args)        {            #region 二进制序列化 BinaryFormatter            //序列化            Person Sheldon = new Person("Sheldon", 26, "重庆市铜梁区--Binary");            using (FileStream fs = new FileStream("Sheldon.bin", FileMode.OpenOrCreate))            {                BinaryFormatter bf = new BinaryFormatter();                bf.Serialize(fs, Sheldon);            }            //反序列化            using (FileStream fs = new FileStream("Sheldon.bin", FileMode.Open))            {                BinaryFormatter bf = new BinaryFormatter();                Person SheldonNew= (Person)bf.Deserialize(fs);                SheldonNew.PrintInfo();            }            #endregion        }    }}


使用SoapFormatter可以将对象序列化为Soap对象以及反序列化。SoapFormatter所在命名空间为using System.Runtime.Serialization.Formatters.Soap;使用时要先添加System.Runtime.Serialization.Formatters.Soap.dll程序集的引用

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using System.Runtime.Serialization.Formatters.Soap;using System.Xml.Serialization;using System.Xml;namespace FirstConsole{    class Program    {        static void Main(string[] args)        {            #region SOAPFormatter            Person Sheldon = new Person("Sheldon", 26, "重庆市铜梁区--Soap");            using (FileStream fs = new FileStream("Sheldon.soap", FileMode.OpenOrCreate))            {                SoapFormatter bf = new SoapFormatter();                bf.Serialize(fs, Sheldon);            }            Console.WriteLine("Serialized in BinaryFormat");            using (FileStream fs = new FileStream("Sheldon.soap", FileMode.Open))            {                SoapFormatter bf = new SoapFormatter();                Person SheldonNew = (Person)bf.Deserialize(fs);                Sheldon.PrintInfo();            }            #endregion        }    }}


 

使用XmlSerializer可以将对象序列化为Xml格式,改类位于using System.Xml.Serialization;使用时先添加System.xml.dll的引用。同时,被序列化的Person对象必须要有无参构造函数。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.IO;using System.Xml.Serialization;using System.Xml;namespace FirstConsole{    class Program    {        static void Main(string[] args)        {            #region XML            Person Sheldon = new Person("Sheldon", 26, "重庆市沙坪坝区--Xml");            using (FileStream fs = new FileStream("Sheldon.xml", FileMode.OpenOrCreate))            {                XmlSerializer xs = new XmlSerializer(typeof(Person));                xs.Serialize(fs, Sheldon);            }            using (FileStream fs = new FileStream("Sheldon.xml", FileMode.Open))            {                XmlSerializer xs = new XmlSerializer(typeof(Person));                Person SheldonNew = (Person)xs.Deserialize(fs);                SheldonNew.PrintInfo();            }            #endregion        }    }}


PS:也可以借助XmlWriter将对象转化为Xml格式的字符串。使用XmlWriter时要引用命名空间using System.Xml;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.IO;using System.Runtime.Serialization.Formatters.Binary;using System.Runtime.Serialization.Formatters.Soap;using System.Xml.Serialization;using System.Xml;namespace FirstConsole{    class Program    {        static void Main(string[] args)        {            #region 将对象序列化为String            StringBuilder builder = new StringBuilder();            Person Sheldon = new Person("Sheldon", 26, "重庆市沙坪坝区--XmlString");            using (XmlWriter writer = XmlWriter.Create(builder))            {                XmlSerializer xs = new XmlSerializer(typeof(Person));                xs.Serialize(writer, Sheldon);            }            Console.WriteLine(builder.ToString());            #endregion        }    }}


 


 

0 0