内存流操作
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.Remoting.Messaging; using System.ServiceModel.Channels; using System.Xml; namespace SoapTest { public class ConvertToSoap { public static void GetSoap(Person person) { SoapFormatter soapFormatter = new SoapFormatter(); using(MemoryStream stream = new MemoryStream()) { soapFormatter.Serialize(stream, person, new Header[] { new Header("Name","abc"), new Header("Password","aaaa") }); byte[] btys = new byte[stream.Length]; btys = stream.ToArray(); string rs = Encoding.UTF8.GetString(btys, 0, btys.Length); stream.Flush(); Console.WriteLine(rs); Console.ReadKey(); } using (XmlTextWriter write = new XmlTextWriter("D://soap.txt", Encoding.UTF8)) { write.Formatting = Formatting.Indented; Message msg = Message.CreateMessage(MessageVersion.Soap12, "http://www.cnblogs.com/",person); msg.WriteMessage(write); } } } }