using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApp
{
public class SerializableTest
{
public void Run()
{
//序列化
Serialize();
//反序列化
Deserialize();
}
/// <summary>
/// 序列化
/// </summary>
private void Serialize()
{
var obj = new TestSimpleObject();
obj.Print();
Stream stream = File.Open("test.xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
//BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Close();
}
/// <summary>
/// 反序列化
/// </summary>
private void Deserialize()
{
var stream = File.Open("test.xml", FileMode.Open);
var formatter = new SoapFormatter();
//BinaryFormatter formatter = new BinaryFormatter();
var obj = (TestSimpleObject)formatter.Deserialize(stream);
obj.Print();
}
}
/// <summary>
/// 简单测试对象
/// </summary>
[Serializable()]
public class TestSimpleObject
{
public string Name { get; set; }
public int Age { get; set; }
public TestSimpleObject()
{
Name = "abeen";
Age = 26;
}
public void Print()
{
Console.WriteLine(string.Format("{0} age is {1}",Name,Age));
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApp
{
public class SerializableTest
{
public void Run()
{
//序列化
Serialize();
//反序列化
Deserialize();
}
/// <summary>
/// 序列化
/// </summary>
private void Serialize()
{
var obj = new TestSimpleObject();
obj.Print();
Stream stream = File.Open("test.xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
//BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Close();
}
/// <summary>
/// 反序列化
/// </summary>
private void Deserialize()
{
var stream = File.Open("test.xml", FileMode.Open);
var formatter = new SoapFormatter();
//BinaryFormatter formatter = new BinaryFormatter();
var obj = (TestSimpleObject)formatter.Deserialize(stream);
obj.Print();
}
}
/// <summary>
/// 简单测试对象
/// </summary>
[Serializable()]
public class TestSimpleObject
{
public string Name { get; set; }
public int Age { get; set; }
public TestSimpleObject()
{
Name = "abeen";
Age = 26;
}
public void Print()
{
Console.WriteLine(string.Format("{0} age is {1}",Name,Age));
}
}
}
text.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:TestSimpleObject id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleApp/ConsoleApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_x003C_Name_x003E_k__BackingField id="ref-3">abeen</_x003C_Name_x003E_k__BackingField>
<_x003C_Age_x003E_k__BackingField>26</_x003C_Age_x003E_k__BackingField>
</a1:TestSimpleObject>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<a1:TestSimpleObject id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleApp/ConsoleApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_x003C_Name_x003E_k__BackingField id="ref-3">abeen</_x003C_Name_x003E_k__BackingField>
<_x003C_Age_x003E_k__BackingField>26</_x003C_Age_x003E_k__BackingField>
</a1:TestSimpleObject>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>