This documentation is archived and is not being maintained.

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 NotInheritable Class SoapFormatter _
	Implements IRemotingFormatter, IFormatter

The SoapFormatter type exposes the following members.

Public methodSoapFormatterInitializes a new instance of the SoapFormatter class with default property values.
Public methodSoapFormatter(ISurrogateSelector, StreamingContext)Initializes a new instance of the SoapFormatter class with the specified ISurrogateSelector and StreamingContext.

Public propertyAssemblyFormatGets or sets the behavior of the deserializer with regards to finding and loading assemblies.
Public propertyBinderGets or sets the SerializationBinder that controls the binding of a serialized object to a type.
Public propertyContextGets or sets the StreamingContext used with this SoapFormatter.
Public propertyFilterLevelGets or sets the TypeFilterLevel of automatic deserialization for .NET Framework remoting.
Public propertySurrogateSelectorGets or sets the SurrogateSelector that controls type substitution during serialization and deserialization.
Public propertyTopObjectGets or sets the ISoapMessage into which the SOAP top object is deserialized.
Public propertyTypeFormatGets or sets the format in which type descriptions are laid out in the serialized stream.

Public methodDeserialize(Stream)Deserializes the data on the provided stream and reconstitutes the graph of objects.
Public methodDeserialize(Stream, HeaderHandler)Deserializes the stream into an object graph with any headers in that stream being handled by the given HeaderHandler.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSerialize(Stream, Object)Serializes an object or graph of objects with the specified root to the given Stream.
Public methodSerialize(Stream, Object, Header())Serializes an object or graph of objects with the specified root to the given Stream in the SOAP Remote Procedure Call (RPC) format.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)


Beginning with the , 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.

Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization

' Note: When building this code, you must reference the
' System.Runtime.Serialization.Formatters.Soap.dll assembly.
Imports System.Runtime.Serialization.Formatters.Soap

Module App

   Sub Main()
   End Sub

   Sub Serialize()
      ' Create a hashtable of values that will eventually be serialized.
      Dim addresses As New Hashtable
      addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052")
      addresses.Add("Fred", "987 Pine Road, Phila., PA 19116")
      addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301")

      ' To serialize the hashtable (and its key/value pairs), 
      ' you must first open a stream for writing.
      ' Use a file stream here.
      Dim fs As New FileStream("DataFile.soap", FileMode.Create)

      ' Construct a SoapFormatter and use it 
      ' to serialize the data to the stream.
      Dim formatter As New SoapFormatter
         formatter.Serialize(fs, addresses)
      Catch e As SerializationException
         Console.WriteLine("Failed to serialize. Reason: " & e.Message)
      End Try
   End Sub

   Sub Deserialize()
      ' Declare the hashtable reference.
      Dim addresses As Hashtable = Nothing

      ' Open the file containing the data that you want to deserialize.
      Dim fs As New FileStream("DataFile.soap", FileMode.Open)
         Dim formatter As New SoapFormatter

         ' Deserialize the hashtable from the file and 
         ' assign the reference to the local variable.
         addresses = DirectCast(formatter.Deserialize(fs), Hashtable)
      Catch e As SerializationException
         Console.WriteLine("Failed to deserialize. Reason: " & e.Message)
      End Try

      ' To prove that the table deserialized correctly, 
      ' display the key/value pairs to the console.
      Dim de As DictionaryEntry
      For Each de In addresses
         Console.WriteLine("{0} lives at {1}.", de.Key, de.Value)
   End Sub
End Module

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

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