如何将Hashtable变量的数据序列化成字符串
答案:1 悬赏:30
解决时间 2021-04-08 22:51
- 提问者网友:江山如画
- 2021-04-08 11:34
如何将Hashtable变量的数据序列化成字符串
最佳答案
- 二级知识专家网友:旧事诱惑
- 2021-04-08 12:29
你把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);
}
}
}
不过要求放置在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);
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |