System.Runtime.Serialization.Formatters.Soap 命名空间包含SoapFormatter 类,该类用于以SOAP 格式将对象序列化和反序列化。
生成使用此命名空间中的类型的应用程序时,必须引用System.Runtime.Serialization.Formatters.Soap.dll 程序集。
using
System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serialization
... {
[Serializable]
public class Car
...{
private string _Price;
private string _Owner;
private string m_filename;
[XmlElement(ElementName = "Price")]
public string Price
...{
get ...{ return this._Price; }
set ...{ this._Price = value; }
}
[XmlElement(ElementName = "Owner")]
public string Owner
...{
get ...{ return this._Owner; }
set ...{ this._Owner = value; }
}
public string Filename
...{
get
...{
return m_filename;
}
set
...{
m_filename=value;
}
}
public Car(string o, string p)
...{
this.Price = p;
this.Owner = o;
}
}
public class cxySerialize
...{
/**//// <summary>
/// 序列化为对象
/// </summary>
/// <param name="objname"></param>
/// <param name="obj"></param>
public static void BinarySerialize(string objname,object obj)
...{
try
...{
string filename=objname+".bin";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
FileStream fileStream = new FileStream(filename, FileMode.Create);
// 用二进制格式序列化
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch(Exception ex)
...{
MessageBox.Show(ex.Source);
}
}
/**//// <summary>
/// 从二进制文件中反序列化
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static object BinaryDeserialize(string objname)
...{
System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
//二进制格式反序列化
string filename=objname+".bin";
if(!System.IO.File.Exists(filename))
throw new Exception("对反序列化之前,请先序列化");
Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
object obj = formatter.Deserialize(stream);
stream.Close();
return obj;
}
/**//// <summary>
/// 序列化为soap 即xml
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static void SoapSerialize(string objname,object obj)
...{
try
...{
string filename=objname+".Soap";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
FileStream fileStream = new FileStream(filename, FileMode.Create);
// 序列化为Soap
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch(Exception ex)
...{
MessageBox.Show(ex.Source);
}
}
/**//// <summary>
/// 反序列号对象
/// </summary>
/// <param name="objname"></param>
public static object SoapDeserialize(string objname)
...{
System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
string filename=objname+".Soap";
//Soap格式反序列化
Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
object obj = formatter.Deserialize(stream);
stream.Close();
return obj;
}
public static void XmlSerialize(string objname,object obj)
...{
try
...{
string filename=objname+".xml";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
FileStream fileStream = new FileStream(filename, FileMode.Create);
// 序列化为xml
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch(Exception ex)
...{
MessageBox.Show(ex.Message);
}
}
/**//// <summary>
/// 从xml序列中反序列化
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static object XmlDeserailize(string objname)
...{
System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
string filename=objname+".xml";
//Xml格式反序列化
Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
object obj = formatter.Deserialize(stream);
stream.Close();
return obj;
}
}
}
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serialization
... {
[Serializable]
public class Car
...{
private string _Price;
private string _Owner;
private string m_filename;
[XmlElement(ElementName = "Price")]
public string Price
...{
get ...{ return this._Price; }
set ...{ this._Price = value; }
}
[XmlElement(ElementName = "Owner")]
public string Owner
...{
get ...{ return this._Owner; }
set ...{ this._Owner = value; }
}
public string Filename
...{
get
...{
return m_filename;
}
set
...{
m_filename=value;
}
}
public Car(string o, string p)
...{
this.Price = p;
this.Owner = o;
}
}
public class cxySerialize
...{
/**//// <summary>
/// 序列化为对象
/// </summary>
/// <param name="objname"></param>
/// <param name="obj"></param>
public static void BinarySerialize(string objname,object obj)
...{
try
...{
string filename=objname+".bin";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
FileStream fileStream = new FileStream(filename, FileMode.Create);
// 用二进制格式序列化
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch(Exception ex)
...{
MessageBox.Show(ex.Source);
}
}
/**//// <summary>
/// 从二进制文件中反序列化
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static object BinaryDeserialize(string objname)
...{
System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
//二进制格式反序列化
string filename=objname+".bin";
if(!System.IO.File.Exists(filename))
throw new Exception("对反序列化之前,请先序列化");
Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
object obj = formatter.Deserialize(stream);
stream.Close();
return obj;
}
/**//// <summary>
/// 序列化为soap 即xml
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static void SoapSerialize(string objname,object obj)
...{
try
...{
string filename=objname+".Soap";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
FileStream fileStream = new FileStream(filename, FileMode.Create);
// 序列化为Soap
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch(Exception ex)
...{
MessageBox.Show(ex.Source);
}
}
/**//// <summary>
/// 反序列号对象
/// </summary>
/// <param name="objname"></param>
public static object SoapDeserialize(string objname)
...{
System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
string filename=objname+".Soap";
//Soap格式反序列化
Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
object obj = formatter.Deserialize(stream);
stream.Close();
return obj;
}
public static void XmlSerialize(string objname,object obj)
...{
try
...{
string filename=objname+".xml";
if(System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
FileStream fileStream = new FileStream(filename, FileMode.Create);
// 序列化为xml
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch(Exception ex)
...{
MessageBox.Show(ex.Message);
}
}
/**//// <summary>
/// 从xml序列中反序列化
/// </summary>
/// <param name="objname"></param>
/// <returns></returns>
public static object XmlDeserailize(string objname)
...{
System.Runtime.Serialization.IFormatter formatter = new SoapFormatter();
string filename=objname+".xml";
//Xml格式反序列化
Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
object obj = formatter.Deserialize(stream);
stream.Close();
return obj;
}
}
}