国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

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

2019-11-11 03:49:48
字體:
供稿:網(wǎng)友
序列化(Serialization):序列化是將對象狀態(tài)轉(zhuǎn)換為可保存或傳輸?shù)钠渌鼣?shù)據(jù)格式的過程。

反序列化(Deserialization) :與序列化相對的是反序列化,將其它數(shù)據(jù)格式轉(zhuǎn)換為對象的過程。

作用:將對象中的數(shù)據(jù)轉(zhuǎn)換成其它文件,方便信息的存儲與交換。

 .NET框架提供了三種序列化的方式:      1、使用BinaryFormatter進行序列化,類中的所有成員變量(甚至標記為 PRivate 的變量)都將被序列化。      2、使用SoapFormatter進行序列化,類中的所有成員變量(甚至標記為 private 的變量)都將被序列化。      3、使用xmlSerializer進行序列化,只有公共字段被序列化。      第一種方式提供了一個簡單的二進制數(shù)據(jù)流以及某些附加的類型信息。而第二種將數(shù)據(jù)流格式化為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="影流"},            };            //創(chuàng)建一個文件流            using (FileStream fs = new FileStream(@"E:/person.txt", FileMode.OpenOrCreate))            {                //創(chuàng)建二進制序列化器                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)        {            //創(chuàng)建一個文件流            using (FileStream fs = new FileStream(@"E:/person.txt", FileMode.Open))            {                //創(chuàng)建二進制序列化器                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));            }        }    }}結(jié)果如圖所示:

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="戰(zhàn)士",age=191,adress="艾歐尼亞"},            new Person(){name="術(shù)士",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));            }        }    }}結(jié)果如圖所示:


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 靖江市| 古交市| 阳新县| 霍山县| 介休市| 武冈市| 金秀| 聂拉木县| 敦化市| 扬州市| 康定县| 金沙县| 乌兰浩特市| 射阳县| 哈密市| 分宜县| 黄龙县| 蕲春县| 浪卡子县| 灌南县| 比如县| 墨竹工卡县| 南开区| 西林县| 晋宁县| 新巴尔虎右旗| 广德县| 南汇区| 余干县| 句容市| 和顺县| 临洮县| 彝良县| 德清县| 石泉县| 衡山县| 商河县| 铁岭市| 丰台区| 应用必备| 女性|