[Serializable]
public class StudentData
{
public string id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
static void Main(string[] args)
{
try
{
StudentData data = new StudentData() { id = "001", name = "西门吹雪", age = 40 };
SaveData(data);
System.Console.WriteLine("end");
}catch(Exception ex)
{
System.Console.WriteLine("Message: " + ex.Message);
System.Console.WriteLine("StackTrace: " + ex.StackTrace);
System.Console.WriteLine("Name: " + ex.TargetSite.Name);
}
System.Console.ReadLine();
}
static void SaveData(object obj)
{
SoapFormatter soapFormatter = new SoapFormatter();
using(Stream stream = new FileStream("data.soap",FileMode.Create,FileAccess.Write,FileShare.None))
{
soapFormatter.Serialize(stream, obj);
}
}
结果:
<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:StudentData id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Frensworkz.ConsoleApplication/Frensworkz.ConsoleApplication%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_x003C_id_x003E_k__BackingField id="ref-3">001</_x003C_id_x003E_k__BackingField>
<_x003C_name_x003E_k__BackingField id="ref-4">西门吹雪</_x003C_name_x003E_k__BackingField>
<_x003C_age_x003E_k__BackingField>40</_x003C_age_x003E_k__BackingField>
</a1:StudentData>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>