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

要序列化的对象(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","重庆市铜梁区--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","重庆市沙坪坝区--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","重庆市沙坪坝区--XmlString");

            using (XmlWriter writer = XmlWriter.Create(builder))
            {
                XmlSerializer xs = new XmlSerializer(typeof(Person));
                xs.Serialize(writer,Sheldon);
            }
            Console.WriteLine(builder.ToString());
            #endregion
        }
    }
}

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...