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

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - 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

 }
}