SoapFormatter Class

Serializes and deserializes an object, or an entire graph of connected objects, in SOAP format.

Namespace:  System.Runtime.Serialization.Formatters.Soap
Assembly:  System.Runtime.Serialization.Formatters.Soap (in System.Runtime.Serialization.Formatters.Soap.dll)

public final class SoapFormatter implements IRemotingFormatter, IFormatter


Beginning with the .NET Framework version 3.5, this class is obsolete. Use BinaryFormatter instead.

The SoapFormatter and BinaryFormatter classes implement the IRemotingFormatter interface to support remote procedure calls (RPCs), and the IFormatter interface (inherited by the IRemotingFormatter) to support serialization of a graph of objects. The SoapFormatter class also supports RPCs with ISoapMessage objects, without using the IRemotingFormatter functionality.

During RPCs, the IRemotingFormatter interface allows the specification of two separate object graphs: the graph of objects to serialize, and an additional graph that contains an array of header objects that convey information about the remote function call (for example, transaction ID or a method signature). For proper serialization, the root object of the first graph must be an object that implements either the IMethodCallMessage interface or the IMethodReturnMessage interface.

During deserialization of an RPC, a HeaderHandler delegate is specified to the Deserialize method of the formatter. The remoting infrastructure uses the HeaderHandler delegate to produce an object that supports the ISerializable interface. This object contains the information stored in the headers, and becomes the root of the graph returned by the deserializer.

The SoapFormatter can also handle RPCs that are produced with objects that implement the ISoapMessage interface. To create an RPC without using the IRemotingFormatter functionality, place an object that supports the ISoapMessage interface at the root of a graph being serialized. To deserialize an RPC created in this manner the TopObject property must be set to another object that supports the ISoapMessage interface, and contains the relevant remote call information.

TimeSpan Serialization

TimeSpan objects are serialized according to the ISO 8601: 1998 section "Alternative" standard.

Version Information

The SoapFormatter does not support serialization compatibility between versions of the .NET Framework. Serialization between versions 1.1 and 2.0 types in the Framework often fails. The following actions can be taken to remedy this issue:

  • Convert to use the BinaryFormatter, which provides compatibility between 1.1 and 2.0.

  • Convert existing persisted data to the new format.

  • Convert all producers and consumers of serialized data to version 2.0.

  • Avoid using types that changed from 1.1 to 2.0.

No code example is currently available or this language may not be supported.
#using <mscorlib.dll>
#using <system.dll>
#using <system.runtime.serialization.formatters.soap.dll>

using namespace System;
using namespace System::IO;
using namespace System::Collections;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Soap;

void Serialize() 
   // Create a hashtable of values that will eventually be serialized.
   Hashtable* addresses = new Hashtable();
   addresses->Add(S"Jeff", S"123 Main Street, Redmond, WA 98052");
   addresses->Add(S"Fred", S"987 Pine Road, Phila., PA 19116");
   addresses->Add(S"Mary", S"PO Box 112233, Palo Alto, CA 94301");

   // To serialize the hashtable (and its keys/values), 
   // you must first open a stream for writing.
   // We will use a file stream here.
   FileStream* fs = new FileStream(S"DataFile.soap", FileMode::Create);

   // Construct a SoapFormatter and use it 
   // to serialize the data to the stream.
   SoapFormatter* formatter = new SoapFormatter();
      formatter->Serialize(fs, addresses);
   catch (SerializationException* e) 
      Console::WriteLine(S"Failed to serialize. Reason: {0}", e->Message);

void Deserialize() 
   // Declare the hashtable reference.
   Hashtable* addresses = 0;

   // Open the file containing the data that we want to deserialize.
   FileStream* fs = new FileStream(S"DataFile.soap", FileMode::Open);
      SoapFormatter* formatter = new SoapFormatter();

      // Deserialize the hashtable from the file and 
      // assign the reference to our local variable.
      addresses = dynamic_cast<Hashtable*>(formatter->Deserialize(fs));
   catch (SerializationException* e) 
      Console::WriteLine(S"Failed to deserialize. Reason: {0}", e->Message);

   // To prove that the table deserialized correctly, 
   // display the keys/values to the console.
   IEnumerator* myEnum = addresses->GetEnumerator();
   while (myEnum->MoveNext()) 
      DictionaryEntry* de = __try_cast<DictionaryEntry*>(myEnum->Current);
      Console::WriteLine(S" {0} lives at {1}.", de->Key, de->Value);

int main() 


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft