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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_32832727/article/details/54896765
序列化(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));
            }
        }
    }
}
结果如图所示:


















猜你喜欢

转载自blog.csdn.net/sinat_32832727/article/details/54896765