asp.net中对象的序列化,方便网络传输

对象序列化 是将对象状态转换为可保持或传输的格式的过程。
反序列化 是将流转换为对象
序列化和反序列化相结合
可以使对象数据轻松的存储和传递

在 .NET 中,如果是对象可序列化,需要在 声明对象的开始部分加上 [Serializable]
这个属性,并且不能被继承
如一个类

[Serializable]
public class
A
{
    public string title;
}

public class B :
A
{
    public int total;
}

则 对象B 不可被序列化


.NET 提供了3种序列化的方式 BinaryFormatter, SoapFormatter和 XmlSerializer
,下面对这3种方式分别以代码形式做介绍

预备工作:
创建一个 实体对象
[Serializable]
public class
Entity
{
    private int _total;
   
private string _title;
    private double
_timeCount;
    private int
_pageCount;

    public Entity()
   
{
    }

    public int
Total
    {
        get
{ return _total; }
        set { _total =
value; }
    }

    public string
Title
    {
        get
{ return _title; }
        set { _title =
value; }
    }

    public double
TimeCount
    {
       
get { return _timeCount; }
        set {
_timeCount = value; }
    }

    public
int PageCount
   
{
        get { return _pageCount;
}
        set { _pageCount = value;
}
    }
}


1. BinaryFormatter
说明: 需要引入命名空间
System.Runtime.Serialization 和
System.Runtime.Serialization.Formatters.Binary

       
//序列化对象
        Entity entity = new
Entity();
        entity.Total =
10;
        entity.Title =
"测试";
        entity.TimeCount =
0.18;
        entity.PageCount =
5;

        IFormatter formatter = new
BinaryFormatter();
        Stream stream =
new FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile.txt",
FileMode.Create, FileAccess.Write,
FileShare.None);

       
formatter.Serialize(stream,
entity);
       
stream.Close();

       
//反序列化对象
        IFormatter formatter1 =
new BinaryFormatter();
        Stream
stream1 = new FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile.txt",
FileMode.Open, FileAccess.Read,
FileShare.Read);
        Entity entity1 =
(Entity)formatter1.Deserialize(stream1);
       
stream1.Close();

       
Response.Write(entity1.Total + "
" + entity1.Title + "
" +
entity1.TimeCount + "
" + entity1.PageCount);

2. SoapFormatter
说明:
需要引入命名空间 System.Runtime.Serialization 和
System.Runtime.Serialization.Formatters.Soap

       
//序列化对象
        Entity entity = new
Entity();
        entity.Total =
10;
        entity.Title =
"测试";
        entity.TimeCount =
0.18;
        entity.PageCount =
5;

        IFormatter formatter = new
SoapFormatter();
        Stream stream = new FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile2.txt", FileMode.Create, FileAccess.Write, FileShare.None);

       
formatter.Serialize(stream,
entity);

       
stream.Close();

       
//反序列化对象
        IFormatter formatter1 =
new SoapFormatter();
        Stream
stream1 = new
FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile2.txt",
FileMode.Open, FileAccess.Read,
FileShare.Read);
        Entity entity1 =
(Entity)formatter1.Deserialize(stream1);
       
stream1.Close();

       
Response.Write(entity1.Total + "
" + entity1.Title + "
" +
entity1.TimeCount + "
" + entity1.PageCount);

3. XmlSerializer
说明:
需要引入命名空间
System.Xml.Serialization

       
//序列化对象
        Entity entity = new
Entity();
        entity.Total =
10;
        entity.Title =
"测试";
        entity.TimeCount =
0.18;
        entity.PageCount =
5;

        XmlSerializer formatter =
new XmlSerializer(typeof(Entity));
        Stream stream = new FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile3.xml", FileMode.Create, FileAccess.Write, FileShare.None);
       
formatter.Serialize(stream,
entity);
       
stream.Close();

       
//反序列化对象
        XmlSerializer formatter1
= new
XmlSerializer(typeof(Entity));
       
Stream stream1 = new
FileStream(@"D:\程序\DotNet技术\PDSource框架\Web\Serializer\myFile3.xml",
FileMode.Open, FileAccess.Read,
FileShare.Read);

       
stream1.Seek(0,
SeekOrigin.Begin);

        Entity
entity1 =
(Entity)formatter1.Deserialize(stream1);
       
stream1.Close();

       
Response.Write(entity1.Total + "
" + entity1.Title + "
" +
entity1.TimeCount + "
" + entity1.PageCount);

posted @ 2016-11-03 22:38  goodTOgreat  阅读(204)  评论(0编辑  收藏  举报