中易网

如何将Hashtable变量的数据序列化成字符串

答案:1  悬赏:30  
解决时间 2021-04-08 22:51
如何将Hashtable变量的数据序列化成字符串
最佳答案
你把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);
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
大吕疃村地址在什么地方,想过去办事
国道每平方米的一般造价是多少?
赤峰市区有几个羽毛球馆?都在哪?
支付宝中的招财宝收益是怎么计算的呢?
最近想开个油画店,很时尚的油画,求大家帮忙
不用网线怎么接收别人家的网线上网
为什么情侣就不能在一个单位?一个部门?
2013年有哪些单招学校有加分政策
《从百草园到三味书屋》三处对比
请谈一谈在生活中遇到的资金时间价值现象(10
最近不知道,很多人都是说蜜舒宝蜜舒宝的,优
国航知音卡能否取消
水浒传中多处写好汉们喝酒吃肉,其中牛肉最多,
三块香皂有三种包装,哪种包装最省包装纸?
东固义村村我想知道这个在什么地方
推荐资讯
昨晚梦到地震了,只有一座楼倒了
"讲文明礼仪 创和谐校园"的演讲稿,急用啊!
you take away missing but not say sorry
光绪元宝部文十二钱
信用卡在最后还款日的第二天还款,会收利息吗?
曼天雨太原分公司东岗路店地址在哪,我要去那
厦门装修公司排名大全?
阅读理解英语 my father died when l was fiv
安以轩和胡歌 为什么近几年都不联系
圣豪音乐广场地址有知道的么?有点事想过去
感觉自己好像人间失格里的叶藏啊。。
为什么我的信用卡能登陆网银却不能付款
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?