你把Hashtable的对象通过序列化处理就可以了。不过要求放置在Hashtable对象中的每个元素对象必须是可以序列化的,否则会失败。下面的代码你可以直接使用:// 类 名 称:SerializationHelper(类数据序列化处理类)
// 功能描述:本类以静态函数的形式实现序列化和反序列化功能
//
// 内    容:增加函数MaskStrToBytes和UnmaskBytesToStr,同时对Serialize后/Deserialize前的数据进行Mask和Unmask处理
//
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Text;namespace Common.Serialize
{
/// <summary>
/// SerializationHelper 的摘要说明。
/// </summary>
public class SerializationHelper
{
//
//函数:Serialize
//类型:byte[]
//参数:data,object类型,要序列化处理的数据类型
//访问:public、static
//功能:对指定对象的数据进行序列化处理,处理完成后的数据存储到指定的文件中。
//其他:
//
//作者:Daview
//日期:2004年08月16日
//
public static byte[] Serialize(object data)
{
BinaryFormatter formatter=new BinaryFormatter();
MemoryStream streamMemory=new MemoryStream();
formatter.Serialize(streamMemory,data);
byte[] binaryData=streamMemory.GetBuffer();
streamMemory.Close();
return binaryData;
}
//
//函数:Deserialize
//类型:object
//参数:binaryData,byte[]类型,要反序列化的数据字节数组
//访问:public、static
//功能:对二进制文件进行反序列化处理,返回反序列化后的数据对象
//其他:
//
//作者:Daview
//日期:2004年08月16日
//
public static object Deserialize(byte[] binaryData)
{
BinaryFormatter formatter=new BinaryFormatter();
MemoryStream streamMemory=new MemoryStream(binaryData);
object data=formatter.Deserialize(streamMemory);
streamMemory.Close();
return data;
}
//
//函数:Serialize
//类型:void
//参数:data,object类型,要序列化处理的数据类型
// filePath,string类型,数据序列化后保存的文件名
//访问:public、static
//功能:对指定对象的数据进行序列化处理,处理完成后的数据存储到指定的文件中。
//其他:
//
//作者:Daview
//日期:2004年06月04日
//
//修改:Daview
//日期:2004年08月16日
//内容:把序列化过程独立出去
//
public static void Serialize(object data, string filePath)
{
string cipherData;
string binaryData;
try
{
//打开文件
System.IO.FileStream fs=new FileStream(filePath,FileMode.Create,FileAccess.Write);
//StreamWriter fs=new StreamWriter(filePath,false);
try
{
//加密数据
binaryData=Convert.ToBase64String(Serialize(data));
cipherData=binaryData;
//cipherData=DataProtection.DataProtection.Encrypt(binaryData,DataProtection.DataProtection.Store.Machine);
//保存数据
byte[] bitTmp=MaskStrToBytes(cipherData);
fs.Write(bitTmp,0,bitTmp.Length);
}
catch(Exception e)
{
ErrInfo.NewErrInfo(e);
}
finally
{
//关闭文件
fs.Flush();
fs.Close();
}
}
catch(Exception e)
{
ErrInfo.NewErrInfo(e);
}
}
//
//函数:Deserialize
//类型:object
//参数:filePath,string类型,要反序列化的数据文件名
//访问:public、static
//功能:对二进制文件进行反序列化处理,返回反序列化后的数据对象
//其他:
//
//作者:Daview
//日期:2004年06月04日
//
//修改:Daview
//日期:2004年08月16日
//内容:把反序列化过程独立出去
//
public static object Deserialize(string filePath)
{
object data=new object(); try
{
//打开文件
FileStream fs=new FileStream(filePath,FileMode.Open,FileAccess.Read);
//StreamReader sr=new StreamReader(filePath);
try
{
//读取二进制文件并转化为字符串
byte[] bitTmp=new byte[fs.Length];
fs.Read(bitTmp,0,bitTmp.Length);
string strBase64Data=UnmaskBytesToStr(bitTmp);
data=Deserialize(Convert.FromBase64String(strBase64Data));
}
catch(Exception e)
{
//数据反序列化失败
ErrInfo.NewErrInfo(e);
data=null;
}
finally
{
//关闭reader
fs.Close();
//sr.Close();
}
}
catch(Exception e)
{
//文件不存在
ErrInfo.NewErrInfo(e);
data=null;
}
return data;
}
//
//函数:MaskStrToBytes
//类型:byte[]
//参数:str,string类型,要进行Mask处理的字符串数据
//访问:private、static
//功能:对字符串转化为字节数组并对所有字节进行Mask处理
//其他:
//
//作者:Daview
//日期:2004年06月05日
//
private static byte[] MaskStrToBytes(string str)
{
byte[] bitTmp=Encoding.ASCII.GetBytes(str);
int iLen=bitTmp.Length;
for(int i=0;i<iLen;i++)
bitTmp[i]^=(byte)0x70;
return bitTmp;
}
//
//函数:UnmaskBytesToStr
//类型:string
//参数:bits,byte[]类型,要进行Unmask处理的字节数组数据
//访问:private、static
//功能:对字节数组进行Unmask处理后再转化为字符串返回
//其他:
//
//作者:Daview
//日期:2004年06月05日
//
private static string UnmaskBytesToStr(byte[] bits)
{
int iLen=bits.Length;
for(int i=0;i<iLen;i++)
bits[i]^=(byte)0x70;
return Encoding.ASCII.GetString(bits);
}
}
}

解决方案 »

  1.   

    谢谢,我试了。你这个对中文处理是错误的。比如:
    Hashtable h1 = new Hashtable();
    h1["a"] = "11大家好,hello!";
    byte[] b1 = Common.Serialize.SerializationHelper.Serialize(h1);
    string str = Common.Serialize.SerializationHelper.UnmaskBytesToStr(b1);
    byte[] b2 = Common.Serialize.SerializationHelper.MaskStrToBytes(str);
    Hashtable h2 = new Hashtable();
    h2 = (Hashtable)Common.Serialize.SerializationHelper.Deserialize(b2);
    这样得到的h1["a"]的中文部分为乱码。
      

  2.   

    谢谢你的提醒。你把里面的编码修改为Default或GB2312等看:Encoding.ASCII
    修改为
    Encoding.Default
      

  3.   

    你好,中文问题解决了,但我刚刚又试了。我序列化这样的数据:
    Hashtable h1 = new Hashtable();
    h1["1"] = "123abc大家好!,!@#";
    h1["2"] = "456efg哈哈,呵呵^_^。!%";
    将上面的h1序列化后的字符串,再反序列化(Deserialize),结果出现错误。Unable to translate bytes [B6] at index 9 from specified code page to Unicode. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Text.DecoderFallbackException: Unable to translate bytes [B6] at index 9 from specified code page to Unicode.Source Error: 
    Line 31:         BinaryFormatter formatter = new BinaryFormatter();
    Line 32:         MemoryStream streamMemory = new MemoryStream(binaryData);
    Line 33:         object data = formatter.Deserialize(streamMemory);
    Line 34:         streamMemory.Close();
    Line 35:         return data;如果我的Hashtable改为下面的只含有一个数据的,就没有问题:
    Hashtable h1 = new Hashtable();
    h1["1"] = "123abc大家好!,!@#";
      

  4.   

    我搞不懂下面这个代码干什么:for (int i = 0; i < iLen; i++)
         bitTmp[i] ^= (byte)0x70;
    请指点,谢谢!
      

  5.   

    搞定了,呵呵,但是还是搞不懂下面这个是什么意思for (int i = 0; i < iLen; i++)
         bitTmp[i] ^= (byte)0x70;