1. 程式人生 > >二進位制流序列化(反序列化)和XML序列化(反序列化)

二進位制流序列化(反序列化)和XML序列化(反序列化)

序列化(Serialization):序列化是將物件狀態轉換為可儲存或傳輸的其它資料格式的過程。

反序列化(Deserialization) :與序列化相對的是反序列化,將其它資料格式轉換為物件的過程。

作用:將物件中的資料轉換成其它檔案,方便資訊的儲存與交換。

 .NET框架提供了三種序列化的方式:
      1、使用BinaryFormatter進行序列化,類中的所有成員變數(甚至標記為 private 的變數)都將被序列化。
      2、使用SoapFormatter進行序列化,類中的所有成員變數(甚至標記為 private 的變數)都將被序列化。
      3、使用XmlSerializer進行序列化,只有公共欄位被序列化。
      第一種方式提供了一個簡單的二進位制資料流以及某些附加的型別資訊。而第二種將資料流格式化為XML儲存。第三種其實和第二種差不多也是XML的格式儲存,只不過比第二種的XML格式要簡化很多(去掉了SOAP特有的額外資訊)。
    1,2必須使用[Serializable]屬性將類標誌為可序列化的,3可以不用對類用[Serializable]屬性進行標記
    1,2可以序列化類中的所有成員變數(私有的,公有的),3只可以序列化類中的公有成員變數。
    如果某個類的元素不想被序列化,  1,2可以使用[NonSerialized]屬性來標誌,3、可以使用[XmlIgnore]來標誌。


1.二進位制流序列化及反序列化

1.1序列化

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace xuliehua
{
    //用[Serializable]標記要序列化的型別
    [Serializable]
    public class Person
    {
        public string name;
        public int age;
        public string adress;
        public void say()
        {
            Console.WriteLine("hello world");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> lp = new List<Person>() { 
            new Person(){name="凱",age=21,adress="影流"},
            new Person(){name="劉",age=23,adress="守望之海"},
            new Person(){name="陸",age=22,adress="征服之海"},
            new Person(){name="星",age=19,adress="艾歐尼亞"},
            new Person(){name="偉",age=24,adress="影流"},
            };
            //建立一個檔案流
            using (FileStream fs = new FileStream(@"E:\person.txt", FileMode.OpenOrCreate))
            {
                //建立二進位制序列化器
                BinaryFormatter bf = new BinaryFormatter();
                //序列化
                bf.Serialize(fs, lp);
            }
        }
    }
}
檢視序列化後的檔案:

1.2反序列化

反序列化需要引用原來的類所在的程式集,如圖所示:


using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace fanxuliehua
{
    class Program
    {
        static void Main(string[] args)
        {
            //建立一個檔案流
            using (FileStream fs = new FileStream(@"E:\person.txt", FileMode.Open))
            {
                //建立二進位制序列化器
                BinaryFormatter bf = new BinaryFormatter();
                //反序列化
                var result = (List<xuliehua.Person>)bf.Deserialize(fs);
                result.ForEach(r => Console.WriteLine(r.name + "\t" + r.age + "\t" + r.adress));
            }
        }
    }
}
結果如圖所示:


2.XML序列化及反序列化

2.1序列化

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace xuliehua
{
    public class Person
    {
        public string name;
        public int age;
        public string adress;
        public void say()
        {
            Console.WriteLine("hello world");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> lp = new List<Person>() { 
            new Person(){name="薩滿",age=211,adress="影流"},
            new Person(){name="獵人",age=231,adress="守望之海"},
            new Person(){name="法師",age=221,adress="征服之海"},
            new Person(){name="戰士",age=191,adress="艾歐尼亞"},
            new Person(){name="術士",age=241,adress="影流"},
            };
            using (FileStream fs = new FileStream(@"E:\person.xml", FileMode.OpenOrCreate))
            {
                XmlSerializer xs = new XmlSerializer(typeof(List<Person>));
                xs.Serialize(fs, lp);
            }
        }
    }
}
檢視序列化後的檔案:

2.2反序列化

同樣,也需要引用原來的類所在的程式集。

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace fanxuliehua
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"E:\person.xml", FileMode.Open))
            {
                XmlSerializer xs = new XmlSerializer(typeof(List<xuliehua.Person>));
                var result = (List<xuliehua.Person>)xs.Deserialize(fs);
                result.ForEach(r => Console.WriteLine(r.name + "\t" + r.age + "\t" + r.adress));
            }
        }
    }
}
結果如圖所示: