C#2.0 序列化与反序列化

加上[Serializable]特性 
/**//// <summary>
/// 序列化
/// </summary>
/// <param name="data">要序列化的对象</param>
public static byte[] Serialize(object data)
...{
    BinaryFormatter formatter = new BinaryFormatter();
    MemoryStream rems = new MemoryStream();
    formatter.Serialize(rems, data);
    return rems.GetBuffer();
}

/**//// <summary>
/// 反序列化
/// </summary>
/// <param name="data">数据缓冲区</param>
/// <returns>对象</returns>
public static object Deserialize(byte[] data)
...{
    BinaryFormatter formatter = new BinaryFormatter();
    MemoryStream rems = new MemoryStream(data);
    return formatter.Deserialize(rems);
}

       private void WriteXml(DataSet ds)
...{
    //将数据写入XML文件
    SaveFileDialog filexml=new SaveFileDialog();
    filexml.Filter="XML文件(*.xml)|*.xml|所有文件(*.*)|*.*";
    if(filexml.ShowDialog()==DialogResult.OK)
    ...{
        ds.WriteXml(filexml.FileName,XmlWriteMode.WriteSchema);
        //ds.WriteXml(filexml.FileName,XmlWriteMode.DiffGram);
        //string s=ds.GetXml();
    }
}

private DataSet ReadXml()
...{
    OpenFileDialog filexml=new OpenFileDialog();
    filexml.Filter="XML文件(*.xml)|*.xml|所有文件(*.*)|*.*";
    if(filexml.ShowDialog()==DialogResult.OK)
    ...{               
        //创建XmlTextReader对象
        XmlTextReader xReader=new XmlTextReader(filexml.FileName);
        //创建一个新的数据集
        DataSet ds=new DataSet();
        //从filexml中读出数据集
        ds.ReadXml(xReader,XmlReadMode.Auto);
        return ds;
    }
}
   #region Binary Serializers
  public   System.IO.MemoryStream SerializeBinary(object request)
  {
   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
   System.IO.MemoryStream memStream = new System.IO.MemoryStream();
   serializer.Serialize(memStream, request);
   return memStream;
  }

  public   object DeSerializeBinary(System.IO.MemoryStream memStream)
  {
    memStream.Position=0;
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    object newobj = deserializer.Deserialize(memStream);
    memStream.Close();
    return newobj;
  }
  #endregion

  #region XML Serializers

  public   System.IO.MemoryStream SerializeSOAP(object request)
  {

           System.Runtime.Serialization.Formatters.Soap.SoapFormatter serializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
           System.IO.MemoryStream memStream = new System.IO.MemoryStream();
           serializer.Serialize(memStream, request);
           return memStream;
  }

  public   object DeSerializeSOAP(System.IO.MemoryStream memStream)
  {
    object sr;
    System.Runtime.Serialization.Formatters.Soap.SoapFormatter deserializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
    memStream.Position=0;
    sr = deserializer.Deserialize(memStream);
    memStream.Close();
    return sr;
  }

  #endregion

你可能感兴趣的:(反序列化)