序列化一:三种方式

天下文章是一家你抄我来我抄他.序列化三种方法

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

1.XML Serialize

2.Soap Serialize

3.Binary Serialize

第一种序列化方式对有些类型不能够序列化,如hashtable;我主要介绍后两种类型得序列化

一.Soap Serialize

使用SoapFormatter.Serialize()实现序列化.SoapFamatter在System.Runtime.Serialization.Formatters.Soap命名空间下,使用时需要引用System.Runtime.Serialization.Formatters.Soap.dll.它可将对象序列化成xml.

[Serializable]
public class Option:ISerializable
{
//此构造函数必须实现,在反序列化时被调用.
public Option(SerializationInfo si, StreamingContext context)
{
this._name=si.GetString("NAME");
this._text=si.GetString("TEXT");
this._att =(MeteorAttributeCollection)si.GetValue("ATT_COLL",typeof(MeteorAttributeCollection));
}
public Option(){_att = new MeteorAttributeCollection();}
public Option(string name,string text,MeteorAttributeCollection att)
{
_name = name;
_text = text;
_att = (att == null ? new MeteorAttributeCollection():att);
}
private string _name;
private string _text;
private MeteorAttributeCollection _att;
///
/// 此节点名称
///
public String Name
{
get{return this._name;}
set{this._name =value;}
}
///
/// 此节点文本值
///
public String Text
{
get{return this._text;}
set{this._text =value;}
}
///
/// 此节点属性
///
public MeteorAttributeCollection AttributeList
{
get{return this._att;}
set{this._att=value;}
}
///此方法必须被实现
public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
{
info.AddValue("NAME",this._name);
info.AddValue("TEXT",this._text);
info.AddValue("ATT_COLL",this._att,typeof(MeteorAttributeCollection));
}
}
在这个类中,红色部分为必须实现的地方.否则在序列化此类的时候会产生异常“必须被标注为可序列化“,“未找到反序列化类型Option类型对象的构造函数“等异常

*****************************

下面是序列化与反序列化类 MeteorSerializer.cs

************************

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;

namespace Maxplatform.Grid
{
///
/// 提供多种序列化对象的方法(SoapSerializer,BinarySerializer)
///
public class MeteorSerializer
{
public MeteorSerializer()
{

}

#region Soap
///
/// Soap格式的序列化
///
///
///
public static string SoapSerializer(object o)
{
// To serialize the hashtable and its key/value pairs,
// you must first open a stream for writing.
// In this case, use a file stream.
//FileStream fs = new FileStream("DataFile.xml", FileMode.Create);
Stream ms=new MemoryStream();
// Construct a BinaryFormatter and use it to serialize the data to the stream.
SoapFormatter formatter = new SoapFormatter();
try
{
formatter.Serialize(ms, o);

byte[] b=new byte[ms.Length];
ms.Position=0;
ms.Read(b,0,b.Length);

string s=Convert.ToBase64String(b);
return s;
}
catch (SerializationException e)
{
//Log.Write("ServiceNode:Exception","Failed to serialize. Reason: " + e.Message);
throw e;
}
finally
{
ms.Close();
}

}
///
/// Soap格式的反序列化
///
///
///
public static object SoapDeserialize(string returnString)
{

// Open the file containing the data that you want to deserialize.
SoapFormatter formatter;
MemoryStream ms=null;
try
{
formatter = new SoapFormatter();

byte[] b=Convert.FromBase64String(returnString);

ms=new MemoryStream(b);

// Deserialize the hashtable from the file and
// assign the reference to the local variable.
object o = formatter.Deserialize(ms);
return o;
}
catch (SerializationException e)
{
//Log.Write("ServiceNode:Exception","Failed to deserialize. Reason: " + e.Message);
throw e;
}
finally
{
ms.Close();

}

}
#endregion

#region Binary
///
/// Binary格式的序列化
///
///
///
public static string BinarySerializer(object o)
{
// To serialize the hashtable and its key/value pairs,
// you must first open a stream for writing.
// In this case, use a file stream.
//FileStream fs = new FileStream("DataFile.xml", FileMode.Create);
Stream ms=new MemoryStream();
// Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(ms, o);

byte[] b=new byte[ms.Length];
ms.Position=0;
ms.Read(b,0,b.Length);

string s=Convert.ToBase64String(b);
return s;
}
catch (SerializationException e)
{
//Log.Write("ServiceNode:Exception","Failed to serialize. Reason: " + e.Message);
throw e ;
}
finally
{
ms.Close();
}

}
///
/// Binary格式的反序列化
///
///
///
public static object BinaryDeserialize(string returnString)
{

// Open the file containing the data that you want to deserialize.
BinaryFormatter formatter;
MemoryStream ms=null;
try
{
formatter = new BinaryFormatter();

byte[] b=Convert.FromBase64String(returnString);

ms=new MemoryStream(b);

// Deserialize the hashtable from the file and
// assign the reference to the local variable.
object response = formatter.Deserialize(ms);
return response;
}
catch (SerializationException e)
{
//Log.Write("ServiceNode:Exception","Failed to deserialize. Reason: " + e.Message);
throw e;
}
finally
{
ms.Close();

}

}
#endregion

}
}

 发表于:2007.08.07 14:19

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12476590/viewspace-134138/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12476590/viewspace-134138/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值