A while ago we’ve seen how to serialize and deserialize objects using XMLSerializer. Somebody asked recently so thought of showing how to do it as SoapFormatter. Here’s the code and you get the idea. One thing to remember is to add reference to Soap Formatters from System.Runtime.Serialization.Formatters.Soap component.
public static string SerializeObjectSoapFormatter(Object objToSerialize)
{
string serialXML = "";
SoapFormatter soapFormatter = new SoapFormatter();
MemoryStream mStream = new MemoryStream();
soapFormatter.Serialize(mStream, objToSerialize);
byte[] buffer = mStream.GetBuffer();
mStream.Close();
UTF8Encoding encoding = new UTF8Encoding();
serialXML = encoding.GetString(buffer);
return serialXML;
}
Love coding!
1 comment:
Thanks for that.
Post a Comment