- 对单个对象序列化,先定义一个接口,有两个泛型方法方法Serialize和Deserialize,分别完成序列化和反序列化,创建泛型方法是为了类型安全。
public interface IGenericFormatter
{
// serializationStream:要序列化为的流。
// graph:位于要序列化图形的根位置的对象。
void Serialize<T>(Stream serializationStream, T graph);
T Deserialize<T>(Stream serializationStream);
}
- 创建一个类GenericFormatter来实现这个接口。
// U:实现IFormatter接口的类
public class GenericFormatter<U> : IGenericFormatter where U : IFormatter, new()
{
private IFormatter m_formatter = new U();
#region IGenericFormatter 成员
public T Deserialize<T>(System.IO.Stream serializationStream)
{
try
{
return (T)m_formatter.Deserialize(serializationStream);
}
catch
{
throw;
}
}
public void Serialize<T>(System.IO.Stream serializationStream, T graph)
{
m_formatter.Serialize(serializationStream, graph);
}
#endregion
}
- 还可以将多个对象序列化到同一个流中,从一个流中反序列化出多个对象。为IGenericFormatter接口添加以下方法。
public interface IGenericFormatter
{
void Serialize<T>(Stream serializationStream, T graph, params object[] args);
object[] Deserialize(Stream serializationStream);
object[] Deserialize(Stream serializationStream, int count);
}
- 实现上边的方法。
public class GenericFormatter<U> : IGenericFormatter where U : IFormatter, new()
{
#region IGenericFormatter 成员
public object[] Deserialize(System.IO.Stream serializationStream)
{
try
{
return Deserialize(serializationStream, -1);
}
catch
{
throw;
}
}
// count:要反序列化对象的个数,如果超出实际个数,将返回包含实际个数的对象数组
public object[] Deserialize(System.IO.Stream serializationStream, int count)
{
ArrayList result = new ArrayList();
try
{
for (int i = 0; count < 0 || i < count; i++)
{
result.Add(Deserialize<object>(serializationStream));
}
}
catch (XmlException) { }
catch { throw; }
return result.ToArray();
}
public void Serialize<T>(System.IO.Stream serializationStream, T graph, params object[] args)
{
Serialize(serializationStream, graph);
foreach (var item in args)
{
Serialize(serializationStream, item);
}
}
#endregion
}
- 单个对象序列化,反序列化实例。
[Serializable]
class Program
{
static void Main(string[] args)
{
Program pro = new Program();
pro.Name = "ceshi1";
GenericFormatter<SoapFormatter> soap = new GenericFormatter<SoapFormatter>();
FileStream stream = new FileStream(@"C:/FlashPast.xml", FileMode.Create, FileAccess.ReadWrite);
soap.Serialize(stream, pro);
// 把流中位置归零。
stream.Seek(0, System.IO.SeekOrigin.Begin);
Program newPro = soap.Deserialize<Program>(stream);
Console.ReadLine();
}
public string Name { get; set; }
}
// FlashPast.xml
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Program id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MyConsoleApplication/MyConsoleApplication%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_x003C_Name_x003E_k__BackingField id="ref-3">
ceshi1
</_x003C_Name_x003E_k__BackingField>
</a1:Program>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
- 多个对象序列化,反序列化实例。
[Serializable]
class Program
{
static void Main(string[] args)
{
Program pro = new Program();
pro.Name = "ceshi1";
Program pro1 = new Program();
pro1.Name = "ceshi2";
GenericFormatter<SoapFormatter> soap = new GenericFormatter<SoapFormatter>();
FileStream stream = new FileStream(@"C:/FlashPast.xml", FileMode.Create, FileAccess.ReadWrite);
soap.Serialize(stream, pro, pro1);
// 把流中位置归零。
stream.Seek(0, System.IO.SeekOrigin.Begin);
object[] arry = soap.Deserialize(stream);
Console.ReadLine();
}
public string Name { get; set; }
}