麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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

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

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

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

 .NET框架提供了三種序列化的方式:      1、使用BinaryFormatter進(jìn)行序列化,類中的所有成員變量(甚至標(biāo)記為 PRivate 的變量)都將被序列化。      2、使用SoapFormatter進(jìn)行序列化,類中的所有成員變量(甚至標(biāo)記為 private 的變量)都將被序列化。      3、使用xmlSerializer進(jìn)行序列化,只有公共字段被序列化。      第一種方式提供了一個(gè)簡(jiǎn)單的二進(jìn)制數(shù)據(jù)流以及某些附加的類型信息。而第二種將數(shù)據(jù)流格式化為XML存儲(chǔ)。第三種其實(shí)和第二種差不多也是XML的格式存儲(chǔ),只不過比第二種的XML格式要簡(jiǎn)化很多(去掉了SOAP特有的額外信息)。    1,2必須使用[Serializable]屬性將類標(biāo)志為可序列化的,3可以不用對(duì)類用[Serializable]屬性進(jìn)行標(biāo)記    1,2可以序列化類中的所有成員變量(私有的,公有的),3只可以序列化類中的公有成員變量。    如果某個(gè)類的元素不想被序列化,  1,2可以使用[NonSerialized]屬性來(lái)標(biāo)志,3、可以使用[XmlIgnore]來(lái)標(biāo)志。

1.二進(jìn)制流序列化及反序列化

1.1序列化

using System;using System.Collections.Generic;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace xuliehua{    //用[Serializable]標(biāo)記要序列化的類型    [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)建一個(gè)文件流            using (FileStream fs = new FileStream(@"E:/person.txt", FileMode.OpenOrCreate))            {                //創(chuàng)建二進(jìn)制序列化器                BinaryFormatter bf = new BinaryFormatter();                //序列化                bf.Serialize(fs, lp);            }        }    }}查看序列化后的文件:

1.2反序列化

反序列化需要引用原來(lái)的類所在的程序集,如圖所示:

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)建一個(gè)文件流            using (FileStream fs = new FileStream(@"E:/person.txt", FileMode.Open))            {                //創(chuàng)建二進(jìn)制序列化器                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反序列化

同樣,也需要引用原來(lái)的類所在的程序集。

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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 九九午夜 | 亚洲第一页中文字幕 | 国产成人精品一区二区视频免费 | 男人的天堂色偷偷 | 爱看久久| 一级色毛片 | 国产精品久久久久久久久久10秀 | 狠狠ri| 欧美成人免费一区二区三区 | 99爱视频在线观看 | 国产一及毛片 | 日韩av成人 | 久久久久久久久久综合 | 欧美18—19sex性护士中国 | 国产在线欧美日韩 | 大奶一级片 | 鲁丝一区二区二区四区 | 在线看小早川怜子av | 日韩视频一区在线 | 日韩字幕 | 高清在线国产 | 内地av在线 | 免费毛片观看 | 国产男女爽爽爽爽爽免费视频 | 日本爽快片100色毛片视频 | 欧美大电影免费观看 | 国产一级毛片网站 | 97超视频在线观看 | 一级黄色影院 | 黄色大片网 | 黄色网址免费在线播放 | 欧美大荫蒂xxx | 激情av在线 | 欧美一级高潮片免费的 | 国产一区免费 | 精品一区二区免费视频视频 | 韩国一级免费视频 | 成人男女激情免费视频 | 国产午夜免费视频 | 婷婷中文字幕一区二区三区 | 欧美成人se01短视频在线看 |