C#中的序列化与反序列化的源代码(三种方法)

第一种序列化的方法:BinaryFormatter(二进制的形式)

 using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

/*
 主要就是调用System.Runtime.Serialization.Formatters.Soap空间下的SoapFormatter类进行序列化和反序列化,
 * 使用之前需要应用System.Runtime.Serialization.Formatters.Soap.dll(.net自带的)
 * 序列化之后的文件是Soap格式的文件(简单对象访问协议(Simple Object Access Protocol,SOAP),
 * 是一种轻量的、简单的、基于XML的协议,它被设计成在WEB上交换结构化的和固化的信息。
 * SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),
 * 简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。
 * 它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。
 * SOAP使用基于XML的数据结构和超文本传输协议(HTTP)的组合定义了一个标准的方法来使用Internet
 * 上各种不同操作环境中的分布式对象。)
 */

/* 主要就是调用System.Runtime.Serialization.Formatters.Binary空间下的【BinaryFormatter类】
 *  进行序列化和反序列化,以缩略型二进制格式写到一个文件中去,速度比较【快】,
 *  而且写入后的文件已二进制保存有一定的【保密】效果。
 */


//这是第一种序列化的方法:BinaryFormatter(二进制的形式)
namespace SerializableTest
{
    //以二进制的形式进行序列化
    public class BinarySerialize
    {
        string strFile = "c://book.data";//以二进制形式序列化
        

        public void Serialize(Book book)
        {
            using (FileStream fs = new FileStream(strFile, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (FileStream fs = new FileStream(strFile, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                book = (Book)formatter.Deserialize(fs);
            }
            return book;
        }
    }
}

=================================================================

第二种方法:SoapFormatter类

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;
//这是第二种序列化的方法:SoapFormatter类
namespace SerializableTest
{
    public class SoapSerialize
    {
        string strFile = "c://BOOK.soap";//以soap形式序列化(针对不同操作环境)


        public void Serialize(Book book)
        {
            using (FileStream fs = new FileStream(strFile, FileMode.Create))
            {
                System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new SoapFormatter();
                formatter.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (FileStream fs = new FileStream(strFile, FileMode.Open))
            {
                System.Runtime.Serialization.Formatters.Soap.SoapFormatter formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
                book = (Book)formatter.Deserialize(fs);
            }
            return book;
        }
    }
}

=========================================================================

第三种方法:XmlSerializer类

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace SerializableTest
{

    public class XmlSerialize
    {
        string strFile = "c://BOOK.xml";//以XML形式序列化

        public void Serialize(Book book)
        {
            using (FileStream fs = new FileStream(strFile, FileMode.Create))
            {
                System.Xml.Serialization.XmlSerializer formatter = new System.Xml.Serialization.XmlSerializer();
                formatter.Serialize(fs, book);
            }
        }

        public Book DeSerialize()
        {
            Book book;
            using (FileStream fs = new FileStream(strFile, FileMode.Open))
            {
                System.Xml.Serialization.XmlSerializer formatter = new System.Xml.Serialization.XmlSerializer();
                book = (Book)formatter.Deserialize(fs);
            }
            return book;
        }
    }
}

=======================================================================

 

以下是对这三种类的调用:一个测试页面 Default.aspx,里面放有两个Button控件.以下是.cs代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace SerializableTest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //序列化
        protected void Button1_Click(object sender, EventArgs e)
        {
            //序列化实体类Book
            //第一种的序列化的方法System.Runtime.Serialization.Formatters.Binary类(保密,速度快)
            //BinarySerialize ssl = new BinarySerialize();
            //第二种序列化的方法System.Runtime.Serialization.Formatters.Soap.SoapFormatter类,前提是需先添加dll的引用;
            //System.Runtime.Serialization.Formatters.Soap
            //SoapSerialize ssl = new SoapSerialize();
            //第三种序列化的方法System.Xml.Serialization.XmlSerializer类
            XmlSerialize ssl = new XmlSerialize();
            Book book = new Book();
            book.BookID = "bookId001";
            book.alBookReader.Add("arrvalue1");
            book.alBookReader.Add("arrvalue2");
            book.strBookName = "C#强化—XML—bookName1";
            book.strBookPwd = "*********001";
            book.SetBookPrice("$ 50000.0");
            ssl.Serialize(book);            
        }

        //反序列化
        protected void Button2_Click(object sender, EventArgs e)
        {
            //反序列化实体类Book
            //第一种的序列化的方法System.Runtime.Serialization.Formatters.Binary类(保密,速度快)
            //BinarySerialize ssl = new BinarySerialize();
            //第二种序列化的方法System.Runtime.Serialization.Formatters.Soap.SoapFormatter类,前提是需先添加dll的引用;
            //System.Runtime.Serialization.Formatters.Soap
            //SoapSerialize ssl = new SoapSerialize();
            //第三种序列化的方法System.Xml.Serialization.XmlSerializer类
            XmlSerialize ssl = new XmlSerialize();           
            Book book = ssl.DeSerialize();
            //遍历输出
            //book.Write();

            mylist.Items.Clear();
            mylist.Items.Add("Book ID:" + book.BookID);
            mylist.Items.Add("Book Name:" + book.strBookName);
            mylist.Items.Add("Book Password:" + book.strBookPwd);
            mylist.Items.Add("Book Price: 受保护XML" );
            for (int i = 0; i < book.alBookReader.Count; i++)
            {
                mylist.Items.Add("Book Reader:" + book.alBookReader[i]);

            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值