惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - kalman

nodejs+express+jade安装备忘 Asp.net MVC应用在IIS7上部署后403错误解决方案 【转】禁止从终端服务器复制文件 FluentData Mysql分页的一个BUG 代码生成器Kalman Studio2.2发布,完美支持Oracle,不需要安装Oracle客户端 【备忘】Oracle常用系统表(做代码生成器用得到) 开发辅助工具Kalman Studio2.0发布,内置基于T4的代码生成器 如何在Yii Framework中使用PHPExcel组件【备忘】 T4代码生成器Kalman Studio发布 如何为Kalman Studio编写T4模板 - kalman - 博客园 发布基于T4模板引擎的代码生成器[Kalman Studio] 请谨慎设置WinForm控件DataGridView列的AutoSizeMode属性 安装apache_2.0.63-win32-x86出现no installed service named "apache2" - kalman 手工查杀Win32/Pacex.Gen,Win32/Genetik,Win32/PSW.Agent.NCC,Win32/PSW.QQPass.VD病毒 win2003下安装Look n Stop网络防火墙导致系统蓝屏(tcpip.sys - address F75F5390 base at F75B4000,DataStamp 4473b09e) 在线播放器代码大全(收藏) - kalman - 博客园 System.Exception: System.Data.OracleClient requires Oracle client software version 8.1.7 or greater 服务器: 消息 15135,级别 16,状态 1,过程 sp_validatepropertyinputs,行 100. 对象无效。不允许在 '.cash_flux' 上使用扩展属性,或对象不存在 Excel组件使用配置文档下载
.NET序列化与反序列化(转)
kalman · 2007-05-01 · via 博客园 - kalman

1.XML Serialize

2.Soap Serialize

3.Binary Serialize

第一种序列化方式对有些类型不能够序列化,如hashtable;我主要介绍后两种类型得序列化

一.Soap Serialize

使用SoapFormatter.Serialize()实现序列化.SoapFamatter在System.Runtime.Serialization.Formatters.Soap命名空间下,使用时需要引用System.Runtime.Serialization.Formatters.Soap.dll.它可将对象序列化成xml.

[Serializable]
  public class Option:ISerializable
  {
//此构造函数必须实现,在反序列化时被调用.
   public Option(SerializationInfo si, StreamingContext context)
   {
    this._name=si.GetString("NAME");
    this._text=si.GetString("TEXT");
    this._att =(MeteorAttributeCollection)si.GetValue("ATT_COLL",typeof(MeteorAttributeCollection));
   }
   public Option(){_att = new MeteorAttributeCollection();}
   public Option(string name,string text,MeteorAttributeCollection att)
   {
    _name = name;
    _text = text;
    _att = (att == null ? new MeteorAttributeCollection():att);
   }
   private string _name;
   private string _text;
   private MeteorAttributeCollection _att;
   /// <summary>
   /// 此节点名称
   /// </summary>
   public String Name
   {
    get{return this._name;}
    set{this._name =value;}
   }
   /// <summary>
   /// 此节点文本值
   /// </summary>
   public String Text
   {
    get{return this._text;}
    set{this._text =value;}
   }
   /// <summary>
   /// 此节点属性
   /// </summary>
   public MeteorAttributeCollection AttributeList
   {
    get{return this._att;}
    set{this._att=value;}
   }
///此方法必须被实现
   public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
   {
    info.AddValue("NAME",this._name);
    info.AddValue("TEXT",this._text);
    info.AddValue("ATT_COLL",this._att,typeof(MeteorAttributeCollection));
   }
}
在这个类中,红色部分为必须实现的地方.否则在序列化此类的时候会产生异常“必须被标注为可序列化“,“未找到反序列化类型Option类型对象的构造函数“等异常

*****************************

下面是序列化与反序列化类 MeteorSerializer.cs

************************

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;

namespace Maxplatform.Grid
{
 /// <summary>
 /// 提供多种序列化对象的方法(SoapSerializer,BinarySerializer)
 /// </summary>
 public class MeteorSerializer
 {
  public MeteorSerializer()
  {
  
  }
 
  #region Soap
  /// <summary>
  /// Soap格式的序列化
  /// </summary>
  /// <param name="o"></param>
  /// <returns></returns>
  public static string SoapSerializer(object o)
  {
   // To serialize the hashtable and its key/value pairs, 
   // you must first open a stream for writing.
   // In this case, use a file stream.
   //FileStream fs = new FileStream("DataFile.xml", FileMode.Create);
   Stream ms=new MemoryStream();
   // Construct a BinaryFormatter and use it to serialize the data to the stream.
   SoapFormatter formatter = new SoapFormatter();
   try
   {
    formatter.Serialize(ms, o);
  
    byte[] b=new byte[ms.Length];
    ms.Position=0;
    ms.Read(b,0,b.Length);
               
    string s=Convert.ToBase64String(b);
    return s;
   }
   catch (SerializationException e)
   {
    //Log.Write("ServiceNode:Exception","Failed to serialize. Reason: " + e.Message);
    throw e;
   }
   finally
   {
    ms.Close();
   }

  }
  /// <summary>
  /// Soap格式的反序列化
  /// </summary>
  /// <param name="returnString"></param>
  /// <returns></returns>
  public static object SoapDeserialize(string returnString)
  {

   // Open the file containing the data that you want to deserialize.
   SoapFormatter formatter;
   MemoryStream ms=null;
   try
   {
    formatter = new SoapFormatter();

    byte[] b=Convert.FromBase64String(returnString);

    ms=new MemoryStream(b);
   
    // Deserialize the hashtable from the file and
    // assign the reference to the local variable.
    object o = formatter.Deserialize(ms);
    return o;
   }
   catch (SerializationException e)
   {
    //Log.Write("ServiceNode:Exception","Failed to deserialize. Reason: " + e.Message);
    throw e;
   }
   finally
   {
    ms.Close();
   
   }

  }
  #endregion

  #region Binary
  /// <summary>
  /// Binary格式的序列化
  /// </summary>
  /// <param name="o"></param>
  /// <returns></returns>
  public static string BinarySerializer(object o)
  {
   // To serialize the hashtable and its key/value pairs, 
   // you must first open a stream for writing.
   // In this case, use a file stream.
   //FileStream fs = new FileStream("DataFile.xml", FileMode.Create);
   Stream ms=new MemoryStream();
   // Construct a BinaryFormatter and use it to serialize the data to the stream.
   BinaryFormatter formatter = new BinaryFormatter();
   try
   {
    formatter.Serialize(ms, o);
  
    byte[] b=new byte[ms.Length];
    ms.Position=0;
    ms.Read(b,0,b.Length);
               
    string s=Convert.ToBase64String(b);
    return s;
   }
   catch (SerializationException e)
   {
    //Log.Write("ServiceNode:Exception","Failed to serialize. Reason: " + e.Message);
    throw e ;
   }
   finally
   {
    ms.Close();
   }

  }
  /// <summary>
  /// Binary格式的反序列化
  /// </summary>
  /// <param name="returnString"></param>
  /// <returns></returns>
  public static object BinaryDeserialize(string returnString)
  {

   // Open the file containing the data that you want to deserialize.
   BinaryFormatter formatter;
   MemoryStream ms=null;
   try
   {
    formatter = new BinaryFormatter();

    byte[] b=Convert.FromBase64String(returnString);

    ms=new MemoryStream(b);
   
    // Deserialize the hashtable from the file and
    // assign the reference to the local variable.
    object response = formatter.Deserialize(ms);
    return response;
   }
   catch (SerializationException e)
   {
    //Log.Write("ServiceNode:Exception","Failed to deserialize. Reason: " + e.Message);
    throw e;
   }
   finally
   {
    ms.Close();
   
   }

  }
  #endregion

 }
}