SoapFormatter Class
Collapse the table of content
Expand the table of content

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)

System.Object
  System.Runtime.Serialization.Formatters.Soap.SoapFormatter

public sealed class SoapFormatter : IRemotingFormatter, IFormatter

NameDescription
System_CAPS_pubmethodSoapFormatter()

Initializes a new instance of the SoapFormatter class with default property values.

System_CAPS_pubmethodSoapFormatter(ISurrogateSelector, StreamingContext)

Initializes a new instance of the SoapFormatter class with the specified ISurrogateSelector and StreamingContext.

NameDescription
System_CAPS_pubpropertyAssemblyFormat

Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.

System_CAPS_pubpropertyBinder

Gets or sets the SerializationBinder that controls the binding of a serialized object to a type.

System_CAPS_pubpropertyContext

Gets or sets the StreamingContext used with this SoapFormatter.

System_CAPS_pubpropertyFilterLevel

Gets or sets the TypeFilterLevel of automatic deserialization for .NET Framework remoting.

System_CAPS_pubpropertySurrogateSelector

Gets or sets the SurrogateSelector that controls type substitution during serialization and deserialization.

System_CAPS_pubpropertyTopObject

Gets or sets the ISoapMessage into which the SOAP top object is deserialized.

System_CAPS_pubpropertyTypeFormat

Gets or sets the format in which type descriptions are laid out in the serialized stream.

NameDescription
System_CAPS_pubmethodDeserialize(Stream)

Deserializes the data on the provided stream and reconstitutes the graph of objects.

System_CAPS_pubmethodDeserialize(Stream, HeaderHandler)

Deserializes the stream into an object graph with any headers in that stream being handled by the given HeaderHandler.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodSerialize(Stream, Object)

Serializes an object or graph of objects with the specified root to the given Stream.

System_CAPS_pubmethodSerialize(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.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_noteNote

Beginning with the .NET Framework 2.0, 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 objects are serialized according to the ISO 8601: 1998 section 5.5.3.2.1 "Alternative" standard.

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.

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;

// Note: When building this code, you must reference the
// System.Runtime.Serialization.Formatters.Soap.dll assembly.
using System.Runtime.Serialization.Formatters.Soap;


class App 
{
    [STAThread]
    static void Main() 
    {
        Serialize();
        Deserialize();
    }

    static void Serialize() 
    {
        // Create a hashtable of values that will eventually be serialized.
        Hashtable addresses = 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.
        FileStream fs = new FileStream("DataFile.soap", FileMode.Create);

        // Construct a SoapFormatter and use it 
        // to serialize the data to the stream.
        SoapFormatter formatter = new SoapFormatter();
        try 
        {
            formatter.Serialize(fs, addresses);
        }
        catch (SerializationException e) 
        {
            Console.WriteLine("Failed to serialize. Reason: " + e.Message);
            throw;
        }
        finally 
        {
            fs.Close();
        }
    }


    static void Deserialize() 
    {
        // Declare the hashtable reference.
        Hashtable addresses  = null;

        // Open the file containing the data that you want to deserialize.
        FileStream fs = new FileStream("DataFile.soap", FileMode.Open);
        try 
        {
            SoapFormatter formatter = new SoapFormatter();

            // Deserialize the hashtable from the file and 
            // assign the reference to the local variable.
            addresses = (Hashtable) formatter.Deserialize(fs);
        }
        catch (SerializationException e) 
        {
            Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
            throw;
        }
        finally 
        {
            fs.Close();
        }

        // To prove that the table deserialized correctly, 
        // display the key/value pairs to the console.
        foreach (DictionaryEntry de in addresses) 
        {
            Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
        }
    }
}

.NET Framework
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft