.net的序列化与反序列化实例

 更新时间:2014年10月17日 11:48:30   投稿:shichen2014  
这篇文章主要介绍了.net的序列化与反序列化实例,详细讲述了.net序列化与反序列化的基本原理与用法实例,具有很好的参考借鉴价值,需要的朋友可以参考下

本文实例讲述了.net的序列化与反序列化的实现方法。分享给大家供大家参考。具体方法如下:

1.序列化与反序列化概述

C#中如果需要:将一个结构很复杂的类的对象存储起来,或者通过网路传输到远程的客户端程序中去,这时就需要用到序列化,反序列化(Serialization & Deserialization)

2.BinaryFormattter

.NET中串行有三种,BinaryFormatter, SoapFormatter和XmlSerializer.

其中BinaryFormattter最简单,它是直接用二进制方式把对象 (Object)进行串行或反串,他的优点是速度快,可以串行private或者protected的member, 在不同版本的。NET中都兼容,可以看作是。NET自己的本命方法,当然缺点也就随之而来了,离开了。NET它就活不了,所以不能在其他平台或跨网路上进 行。

3.序列化

复制代码 代码如下:
BinaryFormatter ser = new BinaryFormatter();
  MemoryStream ms = new MemoryStream();
  ser.Serialize(ms, DS);
  byte[] buffer = ms.ToArray();

  MemoryStream :创建其支持存储区为内存的流

4.反序列化

复制代码 代码如下:
//反序列化:将byte[]型的数据,放到Stream中,BinaryFormatter将流中的数据反序列化成对象
  MemoryStream ms = new MemoryStream(bytes);
  BinaryFormatter ser = new BinaryFormatter();
  DataSetSurrogate dss = ser.Deserialize(ms) asDataSetSurrogate;

5.完整实例:

复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Compression;
using System.IO;

namespace Common
{
    /// <summary>
    /// 利用GzipStream进行压缩和解压
    /// </summary>
    public class GZipUtil
    {
        private static GZipStream gZipStream = null;
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="srcBytes"></param>
        /// <returns></returns>
        public static byte[] Compress(byte[] srcBytes)
        {
            MemoryStream ms = new MemoryStream(srcBytes);
            gZipStream = new GZipStream(ms, CompressionMode.Compress);
            gZipStream.Write(srcBytes, 0, srcBytes.Length);
            gZipStream.Close();
            return ms.ToArray();
        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="srcBytes"></param>
        /// <returns></returns>
        public static byte[] Decompress(byte[] srcBytes)
        {
            MemoryStream srcMs = new MemoryStream(srcBytes);
            gZipStream = new GZipStream(srcMs, CompressionMode.Decompress);
            MemoryStream ms = new MemoryStream();
            byte[] buffer = new byte[40960];
            int n;
            while ((n = gZipStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, n);
            }
            gZipStream.Close();
            return ms.ToArray();
        }

        /// <summary>
        /// 将指定的字节数组压缩,并写入到目标文件
        /// </summary>
        /// <param name="srcBuffer">指定的源字节数组</param>
        /// <param name="destFile">指定的目标文件</param>
        public static void CompressData(byte[] srcBuffer, string destFile)
        {
            FileStream destStream = null;
            GZipStream compressedStream = null;
            try
            {
                //打开文件流
                destStream = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write);
                //指定压缩的目的流(这里是文件流)
                compressedStream = new GZipStream(destStream, CompressionMode.Compress, true);
                //往目的流中写数据,而流将数据写到指定的文件
                compressedStream.Write(srcBuffer, 0, srcBuffer.Length);
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("压缩数据写入文件{0}时发生错误", destFile), ex);
            }
            finally
            {
                // Make sure we allways close all streams              
                if (null != compressedStream)
                {
                    compressedStream.Close();
                    compressedStream.Dispose();
                }

                if (null != destStream)
                    destStream.Close();
            }
        }
        /// <summary>
        /// 将指定的文件解压,返回解压后的数据
        /// </summary>
        /// <param name="srcFile">指定的源文件</param>
        /// <returns>解压后得到的数据</returns>
        public static byte[] DecompressData(string srcFile)
        {
            if (false == File.Exists(srcFile))
                throw new FileNotFoundException(String.Format("找不到指定的文件{0}", srcFile));
            FileStream sourceStream = null;
            GZipStream decompressedStream = null;
            byte[] quartetBuffer = null;
            try
            {
                sourceStream = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);

                decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);

                // Read the footer to determine the length of the destiantion file
                //GZIP文件格式说明:
                //10字节的头,包含幻数、版本号以及时间戳
                //可选的扩展头,如原文件名
                //文件体,包括DEFLATE压缩的数据
                //8字节的尾注,包括CRC-32校验和以及未压缩的原始数据长度(4字节) 文件大小不超过4G

                //为Data指定byte的长度,故意开大byte数据的范围
                //读取未压缩的原始数据长度
                quartetBuffer = new byte[4];
                long position = sourceStream.Length - 4;
                sourceStream.Position = position;
                sourceStream.Read(quartetBuffer, 0, 4);

                int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
                byte[] data;
                if (checkLength <= sourceStream.Length)
                {
                    data = new byte[Int16.MaxValue];
                }
                else
                {
                    data = new byte[checkLength + 100];
                }
                //每100byte从解压流中读出数据,并将读出的数据Copy到Data byte[]中,这样就完成了对数据的解压
                byte[] buffer = new byte[100];

                sourceStream.Position = 0;

                int offset = 0;
                int total = 0;

                while (true)
                {
                    int bytesRead = decompressedStream.Read(buffer, 0, 100);

                    if (bytesRead == 0)
                        break;

                    buffer.CopyTo(data, offset);

                    offset += bytesRead;
                    total += bytesRead;
                }
                //剔除多余的byte
                byte[] actualdata = new byte[total];

                for (int i = 0; i < total; i++)
                    actualdata[i] = data[i];

                return actualdata;
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("从文件{0}解压数据时发生错误", srcFile), ex);
            }
            finally
            {
                if (sourceStream != null)
                    sourceStream.Close();

                if (decompressedStream != null)
                    decompressedStream.Close();
            }
        }

    }
}

6.小结

进行序列化,反序列化,利用到的都是BinaryFormate,都得借普通流MemoryStream,不同的是:

序列化时,将对象序列化后放到MemoryStream,而反序列化时,将MemoryStream中的byte[]数据,反序列成对象

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • C#使用winform实现进度条效果

    C#使用winform实现进度条效果

    这篇文章主要为大家详细介绍了C#使用winform实现进度条效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • c#中虚函数的相关使用方法

    c#中虚函数的相关使用方法

    若一个实例方法声明前带有virtual关键字,那么这个方法就是虚方法。
    2013-02-02
  • C#实现将Doc文档转换成rtf格式的方法示例

    C#实现将Doc文档转换成rtf格式的方法示例

    这篇文章主要介绍了C#实现将Doc文档转换成rtf格式的方法,结合实例形式分析了C#针对word文件的读取及文档格式转换相关操作技巧,需要的朋友可以参考下
    2017-07-07
  • C#设置Word文本框中改变文字方向的方法

    C#设置Word文本框中改变文字方向的方法

    在Word中可插入文本框,默认情况下插入的文本框中的文字方向为横向排列,对于一些特殊文档的设计要求,需要改变文字方向,本文就详细的介绍一下使用,感兴趣的可以了解一下
    2021-06-06
  • C#生成随机数实例

    C#生成随机数实例

    这篇文章主要介绍了C#生成随机数的方法,实例分析了随机数的生成原理与使用技巧,需要的朋友可以参考下
    2015-01-01
  • C#使用xsd文件验证XML格式是否正确的实现方法

    C#使用xsd文件验证XML格式是否正确的实现方法

    这篇文章主要介绍了C#使用xsd文件验证XML格式是否正确的实现方法,结合实例形式分析了C#针对xml文件的创建、验证相关操作技巧,需要的朋友可以参考下
    2017-01-01
  • C#窗体控件DataGridView常用设置

    C#窗体控件DataGridView常用设置

    这篇文章主要为大家详细介绍了C#窗体控件DataGridView常用10项设置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • C#利用反射技术实现去掉按钮选中时的边框效果

    C#利用反射技术实现去掉按钮选中时的边框效果

    这篇文章主要介绍了C#利用反射技术实现去掉按钮选中时的边框效果,涉及C#针对窗口的参数设置技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • C#图像处理之霓虹效果实现方法

    C#图像处理之霓虹效果实现方法

    这篇文章主要介绍了C#图像处理之霓虹效果实现方法,可实现图片转换成霓虹效果的功能,需要的朋友可以参考下
    2015-04-04
  • Unity UGUI的InputField输入框组件使用详解

    Unity UGUI的InputField输入框组件使用详解

    这篇文章主要为大家介绍了Unity UGUI的InputField输入框组件使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07

最新评论