反序列化和序列化

将对象转换为字节序列的过程称为对象的序列化。

将字节序列恢复为对象的过程称为对象的反序列化。

1)它可以将对象序列化成字节永久保存在硬盘中。

2)在网络上传送对象的字节序列。

.net为我们提供了三种序列化方式:

【1】、XML Serializer

【2】、SOP Serializer

【3】、BinarySerializer

要使一个类可以序列化,必须对类标记Serializable。

例子:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.IO;

using System.Dynamic;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using System.Xml.Serialization;



namespace ConsoleApplication2

{

    class Program

    {

       

        static void Main(string[] args)

        {

            Class1 c = new Class1();

            c.id = 1;

            c.uname = "admin";

            c.upwd = "admin";

            filepath = @"E:\ff.xml";

            XMLXMLSeria(c);

            Class1 c1 = XMLUnSeria();

            

        }



        /// <summary>

        /// 二进制序列化

        /// </summary>

        /// <param name="cc">标记为序列化的类</param>

        static void Seria(Class1 cc)

        {

            File.WriteAllText(filepath, "");

            IFormatter format = new BinaryFormatter();

            Stream str = new FileStream(filepath, FileMode.Append, FileAccess.Write, FileShare.None);

            format.Serialize(str, cc);

            str.Close();

        }



        /// <summary>

        /// 二进制反序列化

        /// </summary>

        /// <returns>返回序列化的类</returns>

        static Class1 UnSeria()

        {

            IFormatter format = new BinaryFormatter();

            Stream str = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.None);

            Class1 cd = (Class1)format.Deserialize(str);

            str.Close();

            return cd;

        }



        /// <summary>

        /// xml序列化

        /// </summary>

        /// <param name="cc">标记为序列化的类</param>

        static void XMLXMLSeria(Class1 cc)

        {

            File.WriteAllText(filepath, "");

            XmlSerializer format = new XmlSerializer(cc.GetType());

            Stream str = new FileStream(filepath, FileMode.Append, FileAccess.Write, FileShare.None);

            format.Serialize(str, cc);

            str.Close();

        }



        /// <summary>

        /// xml反序列化

        /// </summary>

        /// <returns>返回序列化的类</returns>

        static Class1 XMLUnSeria()

        {

            Class1 cd = new Class1();

            XmlSerializer format = new XmlSerializer(cd.GetType());

            Stream str = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.None);

            cd = (Class1)format.Deserialize(str);

            str.Close();

            return cd;

        }



        public static string filepath

        {

            get;

            set;

        }

     

      

    }

}



 

class1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace ConsoleApplication2

{

    [Serializable]

   public class Class1

    {

        public int id

        {

            get;

            set;

        }

        public string uname

        {

            get;

            set;

        }

        public string upwd

        {

            get;

            set;

        }



    }

}



 

二进制的bin序列化速度最快。

你可能感兴趣的:(反序列化)