SoapFormatter SoapFormatter SoapFormatter SoapFormatter Class

定義

以 SOAP 格式序列化和還原序列化物件,或連接之物件的整個圖形。Serializes and deserializes an object, or an entire graph of connected objects, in SOAP format.

public ref class SoapFormatter sealed : System::Runtime::Remoting::Messaging::IRemotingFormatter
public sealed class SoapFormatter : System.Runtime.Remoting.Messaging.IRemotingFormatter
type SoapFormatter = class
    interface IRemotingFormatter
    interface IFormatter
Public NotInheritable Class SoapFormatter
Implements IRemotingFormatter
繼承
SoapFormatterSoapFormatterSoapFormatterSoapFormatter
實作

範例

#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 = gcnew 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 keys/values), 
   // you must first open a stream for writing.
   // We will use a file stream here.
   FileStream^ fs = gcnew FileStream( "DataFile.soap",FileMode::Create );
   
   // Construct a SoapFormatter and use it 
   // to serialize the data to the stream.
   SoapFormatter^ formatter = gcnew SoapFormatter;
   try
   {
      formatter->Serialize( fs, addresses );
   }
   catch ( SerializationException^ e ) 
   {
      Console::WriteLine( "Failed to serialize. Reason: {0}", e->Message );
      throw;
   }
   finally
   {
      fs->Close();
   }

}

void Deserialize()
{
   
   // Declare the hashtable reference.
   Hashtable^ addresses = nullptr;
   
   // Open the file containing the data that we want to deserialize.
   FileStream^ fs = gcnew FileStream( "DataFile.soap",FileMode::Open );
   try
   {
      SoapFormatter^ formatter = gcnew 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( "Failed to deserialize. Reason: {0}", e->Message );
      throw;
   }
   finally
   {
      fs->Close();
   }

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


[STAThread]
int main()
{
   Serialize();
   Deserialize();
}

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);
        }
    }
}
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()
      Serialize()
      Deserialize()
   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
      Try
         formatter.Serialize(fs, addresses)
      Catch e As SerializationException
         Console.WriteLine("Failed to serialize. Reason: " & e.Message)
         Throw
      Finally
         fs.Close()
      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)
      Try
         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)
         Throw
      Finally
         fs.Close()
      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)
      Next
   End Sub
End Module

備註

注意

從.NET Framework 2.0 開始,這個類別已經過時。Beginning with the .NET Framework 2.0, this class is obsolete. 請改用 BinaryFormatterUse BinaryFormatter instead.

SoapFormatterBinaryFormatter類別會實作IRemotingFormatter介面,以支援遠端程序呼叫 (Rpc),而IFormatter介面 (繼承IRemotingFormatter) 以支援物件圖形序列化。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. SoapFormatter類別也支援使用 RpcISoapMessage物件,而不需使用IRemotingFormatter功能。The SoapFormatter class also supports RPCs with ISoapMessage objects, without using the IRemotingFormatter functionality.

Rpc 期間IRemotingFormatter介面可讓兩個不同的物件圖形的規格: 要序列化的物件圖形,以及包含傳達 (適用於遠端函式呼叫的相關資訊的標頭物件的陣列的其他圖形例如,交易識別碼或方法簽章)。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). 進行適當的序列化,第一個圖形的根物件必須實作的物件IMethodCallMessage介面或IMethodReturnMessage介面。For proper serialization, the root object of the first graph must be an object that implements either the IMethodCallMessage interface or the IMethodReturnMessage interface.

在 RPC,還原序列化期間HeaderHandler來指定委派Deserialize格式子的方法。During deserialization of an RPC, a HeaderHandler delegate is specified to the Deserialize method of the formatter. 使用遠端基礎結構HeaderHandler委派產生支援的物件ISerializable介面。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.

SoapFormatter也可以處理與實作的物件所產生的 RpcISoapMessage介面。The SoapFormatter can also handle RPCs that are produced with objects that implement the ISoapMessage interface. 若要建立而不使用 RPCIRemotingFormatter功能,支援的物件位置ISoapMessage序列化圖形的根目錄中的介面。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. 若要還原序列化以這種方式建立 RPCTopObject屬性必須設定為支援的另一個物件ISoapMessage介面,並包含相關的遠端呼叫資訊。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 序列化TimeSpan Serialization

TimeSpan 物件會序列化根據 ISO 8601:1998 區段 5.5.3.2.1 「 替代 」 標準。TimeSpan objects are serialized according to the ISO 8601: 1998 section 5.5.3.2.1 "Alternative" standard.

版本資訊Version Information

SoapFormatter不支援序列化的.NET framework 版本之間的相容性。The SoapFormatter does not support serialization compatibility between versions of the .NET Framework. Framework 通常 1.1 和 2.0 版別之間的序列化會失敗。Serialization between versions 1.1 and 2.0 types in the Framework often fails. 若要修正此問題,可以採取下列動作:The following actions can be taken to remedy this issue:

  • 若要使用的轉換BinaryFormatter,以提供 1.1 和 2.0 之間的相容性。Convert to use the BinaryFormatter, which provides compatibility between 1.1 and 2.0.

  • 將現有的保存的資料轉換成新格式。Convert existing persisted data to the new format.

  • 將所有的產生者和序列化資料的取用者轉換到 2.0 版。Convert all producers and consumers of serialized data to version 2.0.

  • 避免使用從 1.1 版變更為 2.0 的類型。Avoid using types that changed from 1.1 to 2.0.

建構函式

SoapFormatter() SoapFormatter() SoapFormatter() SoapFormatter()

使用預設屬性值,初始化 SoapFormatter 類別的新執行個體。Initializes a new instance of the SoapFormatter class with default property values.

SoapFormatter(ISurrogateSelector, StreamingContext) SoapFormatter(ISurrogateSelector, StreamingContext) SoapFormatter(ISurrogateSelector, StreamingContext) SoapFormatter(ISurrogateSelector, StreamingContext)

使用指定的 SoapFormatterISurrogateSelector,初始化 StreamingContext 類別的新執行個體。Initializes a new instance of the SoapFormatter class with the specified ISurrogateSelector and StreamingContext.

屬性

AssemblyFormat AssemblyFormat AssemblyFormat AssemblyFormat

取得或設定有關尋找和載入組件之還原序列化程式的行為。Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.

Binder Binder Binder Binder

取得或設定控制將序列化物件繫結至型別的 SerializationBinderGets or sets the SerializationBinder that controls the binding of a serialized object to a type.

Context Context Context Context

取得或設定與此 StreamingContext 一起使用的 SoapFormatterGets or sets the StreamingContext used with this SoapFormatter.

FilterLevel FilterLevel FilterLevel FilterLevel

取得或設定 .NET Framework 遠端之自動還原序列化的 TypeFilterLevelGets or sets the TypeFilterLevel of automatic deserialization for .NET Framework remoting.

SurrogateSelector SurrogateSelector SurrogateSelector SurrogateSelector

取得或設定在序列化和還原序列化期間控制型別替代的 SurrogateSelectorGets or sets the SurrogateSelector that controls type substitution during serialization and deserialization.

TopObject TopObject TopObject TopObject

取得或設定 ISoapMessage,做為 SOAP 上層物件還原序列化的目標。Gets or sets the ISoapMessage into which the SOAP top object is deserialized.

TypeFormat TypeFormat TypeFormat TypeFormat

取得或設定型別描述在已序列化資料流中的配置格式。Gets or sets the format in which type descriptions are laid out in the serialized stream.

方法

Deserialize(Stream) Deserialize(Stream) Deserialize(Stream) Deserialize(Stream)

還原序列化在提供的資料流上的資料,並重新組合物件 Graph。Deserializes the data on the provided stream and reconstitutes the graph of objects.

Deserialize(Stream, HeaderHandler) Deserialize(Stream, HeaderHandler) Deserialize(Stream, HeaderHandler) Deserialize(Stream, HeaderHandler)

將資料流還原序列化至物件圖形中,使用指定的 HeaderHandler 處理該資料流中的任何標頭。Deserializes the stream into an object graph with any headers in that stream being handled by the given HeaderHandler.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Serialize(Stream, Object) Serialize(Stream, Object) Serialize(Stream, Object) Serialize(Stream, Object)

使用指定的根,將物件或物件的圖形序列化至指定的 StreamSerializes an object or graph of objects with the specified root to the given Stream.

Serialize(Stream, Object, Header[]) Serialize(Stream, Object, Header[]) Serialize(Stream, Object, Header[]) Serialize(Stream, Object, Header[])

使用指定的根,將物件或物件圖形序列化至 SOAP 遠端程序呼叫 (RPC) 格式的指定 StreamSerializes an object or graph of objects with the specified root to the given Stream in the SOAP Remote Procedure Call (RPC) format.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於