浅谈C# XML WebServer数据序列化及数据压缩
来源:互联网 发布:淘宝店铺保证金30元 编辑:程序博客网 时间:2024/04/20 20:53
由于公司近期业务的需要,本要研究一些有关 XML WebServer下数据序列化及数据压缩的实现问题,现与大家分享一下,也希望大家勇于讨论,互相学习。由于要实现网络传输入,考虑到操作系统、网络性能等几方面的原因,就需要对数据实现序列化和数据压缩传输入。本人简单地实验了一下,如果在XML WebServer下直接使用DataSet传输入数据,就算实现压缩,也是非常具大的。最好的方法是自己定义好结构体或都类对象(当然在定义的时间要声明我可序列化的哟),进行序列化,然后再进行数据压缩;到了另外一端时选进行解压缩,然后再反序列化。通过这样的实现,从我的项目经验得到的结论是,大大地提高了数据传输的性能,尤其是对于数据量具大的数据传输入更是如此。本人已把数据对象序列化和反序列化、压缩和解压方法(引用的是开源插件,可以从网上http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 下载)封装成一个完整的类中了,代码如文中所述,如有不懂不解之处希望各位一起讨论,也希望能听到更好的见解。
/**//*********************************************************************************************
* 文 件 名:ObjectBinaryFormate.cs
* 创 建 人:王峰
* 功能说明:实现对象序列化与反序列
* 创建日期:2006-12-25
* 单位名称:华域科技
* 版 本:1.0
* 更新说明:---------------------------------------------------------------------------------
* 修 改 人:王峰
* 日 期:2007-01-24
* 修改说明:实现数据的压缩
* -------------------------------------------------------------------------------------------
* 其它说明:无
***********************************************************************************************/
using System;
using System.IO;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
//此处需要进相关的网站下载相应的开源插件
using ICSharpCode.SharpZipLib.Zip.Compression;
namespace SmsClientApp
...{
/**//// <summary>
/// ObjectBinaryFormate 的摘要说明。
/// </summary>
public class ObjectBinaryFormate
...{
public ObjectBinaryFormate()
...{
}
/**//**********************************************************************************************
* 方法名称:ChangeObjectToBytes
* 功能说明:把数据对象序列化为字节型数组
* 输 入 值:数据对象
* 输 出 值:无
* 返 回 值:字节数组
* 其它说明:无
**********************************************************************************************/
public static byte[] ChangeObjectToBytes(object objValue)
...{
byte[] dataValue = null;
try
...{
//序列化
BinaryFormatter formate = new BinaryFormatter();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
formate.Serialize(smsStream, objValue);
dataValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("序列化失败!"+e.Message);
}
//返回压缩后的数据
return CompressByteData(dataValue);
}
/**//**********************************************************************************************
* 方法名称:ChangeBytesToObject
* 功能说明:把字节型数组反序列为数据对象
* 输 入 值:字节数组
* 输 出 值:无
* 返 回 值:数据对象
* 其它说明:无
**********************************************************************************************/
public static object ChangeBytesToObject(byte[] dataValue)
...{
object objValue = null;
try
...{
//解压缩数据
byte[] resultValue = DecompressByteData(dataValue);
//反序列化
BinaryFormatter formate = new BinaryFormatter();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
smsStream.Write(resultValue, 0, resultValue.Length);
//指针归零
smsStream.Seek(0, SeekOrigin.Begin);
objValue = (object)formate.Deserialize(smsStream);
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("反序列化失败!"+e.Message);
}
//返回对象
return objValue;
}
/**//**********************************************************************************************
* 方法名称:CompressByteData
* 功能说明:数据压缩
* 输 入 值:源字节数据
* 输 出 值:无
* 返 回 值:压缩后的字节数据
* 其它说明:无
**********************************************************************************************/
private static byte[] CompressByteData(Byte[] dataValue)
...{
byte[] resultValue = null;
try
...{
//压缩数据
Deflater compressFile = new Deflater(Deflater.BEST_COMPRESSION);
compressFile.SetInput(dataValue);
compressFile.Finish();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!compressFile.IsFinished)
...{
int bufLength = compressFile.Deflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("压缩数据失败!"+e.Message);
}
//返回压缩后的数据
return resultValue;
}
/**//**********************************************************************************************
* 方法名称:DecompressByteData
* 功能说明:解压缩数据
* 输 入 值:源字节数据
* 输 出 值:无
* 返 回 值:解压后数据
* 其它说明:无
**********************************************************************************************/
private static byte[] DecompressByteData(byte[] dataValue)
...{
byte[] resultValue = null;
try
...{
//解压缩数据
Inflater decompressFile = new Inflater();
decompressFile.SetInput(dataValue);
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!decompressFile.IsFinished)
...{
int bufLength = decompressFile.Inflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("解压缩数据失败!"+e.Message);
}
//返回数据
return resultValue;
}
}
}
* 文 件 名:ObjectBinaryFormate.cs
* 创 建 人:王峰
* 功能说明:实现对象序列化与反序列
* 创建日期:2006-12-25
* 单位名称:华域科技
* 版 本:1.0
* 更新说明:---------------------------------------------------------------------------------
* 修 改 人:王峰
* 日 期:2007-01-24
* 修改说明:实现数据的压缩
* -------------------------------------------------------------------------------------------
* 其它说明:无
***********************************************************************************************/
using System;
using System.IO;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
//此处需要进相关的网站下载相应的开源插件
using ICSharpCode.SharpZipLib.Zip.Compression;
namespace SmsClientApp
...{
/**//// <summary>
/// ObjectBinaryFormate 的摘要说明。
/// </summary>
public class ObjectBinaryFormate
...{
public ObjectBinaryFormate()
...{
}
/**//**********************************************************************************************
* 方法名称:ChangeObjectToBytes
* 功能说明:把数据对象序列化为字节型数组
* 输 入 值:数据对象
* 输 出 值:无
* 返 回 值:字节数组
* 其它说明:无
**********************************************************************************************/
public static byte[] ChangeObjectToBytes(object objValue)
...{
byte[] dataValue = null;
try
...{
//序列化
BinaryFormatter formate = new BinaryFormatter();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
formate.Serialize(smsStream, objValue);
dataValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("序列化失败!"+e.Message);
}
//返回压缩后的数据
return CompressByteData(dataValue);
}
/**//**********************************************************************************************
* 方法名称:ChangeBytesToObject
* 功能说明:把字节型数组反序列为数据对象
* 输 入 值:字节数组
* 输 出 值:无
* 返 回 值:数据对象
* 其它说明:无
**********************************************************************************************/
public static object ChangeBytesToObject(byte[] dataValue)
...{
object objValue = null;
try
...{
//解压缩数据
byte[] resultValue = DecompressByteData(dataValue);
//反序列化
BinaryFormatter formate = new BinaryFormatter();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
smsStream.Write(resultValue, 0, resultValue.Length);
//指针归零
smsStream.Seek(0, SeekOrigin.Begin);
objValue = (object)formate.Deserialize(smsStream);
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("反序列化失败!"+e.Message);
}
//返回对象
return objValue;
}
/**//**********************************************************************************************
* 方法名称:CompressByteData
* 功能说明:数据压缩
* 输 入 值:源字节数据
* 输 出 值:无
* 返 回 值:压缩后的字节数据
* 其它说明:无
**********************************************************************************************/
private static byte[] CompressByteData(Byte[] dataValue)
...{
byte[] resultValue = null;
try
...{
//压缩数据
Deflater compressFile = new Deflater(Deflater.BEST_COMPRESSION);
compressFile.SetInput(dataValue);
compressFile.Finish();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!compressFile.IsFinished)
...{
int bufLength = compressFile.Deflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("压缩数据失败!"+e.Message);
}
//返回压缩后的数据
return resultValue;
}
/**//**********************************************************************************************
* 方法名称:DecompressByteData
* 功能说明:解压缩数据
* 输 入 值:源字节数据
* 输 出 值:无
* 返 回 值:解压后数据
* 其它说明:无
**********************************************************************************************/
private static byte[] DecompressByteData(byte[] dataValue)
...{
byte[] resultValue = null;
try
...{
//解压缩数据
Inflater decompressFile = new Inflater();
decompressFile.SetInput(dataValue);
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!decompressFile.IsFinished)
...{
int bufLength = decompressFile.Inflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("解压缩数据失败!"+e.Message);
}
//返回数据
return resultValue;
}
}
}
- 浅谈C# XML WebServer数据序列化及数据压缩
- 浅谈C# XML WebServer数据序列化及数据压缩
- C# webserver 中对象的序列化
- C# 序列化xml数据到class
- c#序列化XML
- C# 序列化 xml
- XML序列化 C#
- 序列化 XML 数据
- C# 对象XML序列化
- C# xml序列化类
- c# 序列化 部分 XML
- C# XML序列化小结
- C# XML序列化去掉XML默认的命名空间及声明头
- .Net序列化与数据压缩类 (DataSet)
- C# 数据序列化类
- C#之数据序列化
- Android中使用xml保存数据(xml文件序列化)及使用pull解析xml文件
- C#实现对象的Xml格式序列化及反序列化
- Visual C#常用函数和方法集汇总
- C# 编码规范和编程好习惯
- 个人简历
- 适配器模式(adapter)
- C#入门代码
- 浅谈C# XML WebServer数据序列化及数据压缩
- 有点疑惑的地方
- C#实现类似qq的屏幕截图程序
- 春节小偷过年,手机放在办公桌上被偷
- ASP连接数据库的全能代码
- 详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
- 谁都想拥有完美的界面,它可以给你一些启示
- asp.net 取得远程的IP地址和浏览器类型
- 一个往环境变量path中添加路径的工具