共用方式為


SoapFormatter 類別

定義

以 SOAP 格式序列化和還原序列化物件,或連接之物件的整個圖形。

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
繼承
SoapFormatter
實作

範例

#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 開始,這個類別已過時。

警告

BinaryFormatter 不安全且無法確保安全。 如需詳細資訊,請參閱 BinaryFormatter 安全性指南

SoapFormatterBinaryFormatter 類別會實作 IRemotingFormatter 介面來支援遠端過程調用, (RPC) ,以及 IFormatter) 繼承的IRemotingFormatter介面 (,以支援物件的圖形串行化。 類別 SoapFormatter 也支援具有 ISoapMessage 物件的 RPC,而不需使用 IRemotingFormatter 功能。

在 RPC 期間, IRemotingFormatter 介面允許指定兩個不同的物件圖形:要串行化物件的圖形,以及包含標頭物件陣列的其他圖形,這些物件會傳達遠端函數呼叫的相關信息 (,例如交易標識符或方法簽章) 。 若要進行適當的串行化,第一個圖形的根對象必須是實 IMethodCallMessage 作 介面或 IMethodReturnMessage 介面的物件。

在 RPC 還原串行化期間, HeaderHandler 會將委派指定給 Deserialize 格式子的方法。 遠端基礎結構會 HeaderHandler 使用 委派來產生支援 ISerializable 介面的物件。 此物件包含儲存在標頭中的資訊,並成為還原串行化程式所傳回之圖形的根目錄。

SoapFormatter也可以處理與實ISoapMessage作 介面之 物件一起產生的 RPC。 若要在不使用功能的情況下 IRemotingFormatter 建立 RPC,請將支援 ISoapMessage 介面的物件放在要串行化之圖形的根目錄。 若要還原串行化以此方式建立的 RPC, TopObject 屬性必須設定為支援 介面的另一個物件 ISoapMessage ,並包含相關的遠端呼叫資訊。

TimeSpan 物件會根據 ISO 8601:1998 第 5.5.3.2.1 節「替代」標準進行串行化。

建構函式

SoapFormatter()

使用預設屬性值,初始化 SoapFormatter 類別的新執行個體。

SoapFormatter(ISurrogateSelector, StreamingContext)

使用指定的 SoapFormatterISurrogateSelector,初始化 StreamingContext 類別的新執行個體。

屬性

AssemblyFormat

取得或設定有關尋找和載入組件之還原序列化程式的行為。

Binder

取得或設定控制將序列化物件繫結至型別的 SerializationBinder

Context

取得或設定與此 StreamingContext 一起使用的 SoapFormatter

FilterLevel

取得或設定 .NET Framework 遠端之自動還原序列化的 TypeFilterLevel

SurrogateSelector

取得或設定在序列化和還原序列化期間控制型別替代的 SurrogateSelector

TopObject

取得或設定 ISoapMessage,做為 SOAP 上層物件還原序列化的目標。

TypeFormat

取得或設定型別描述在已序列化資料流中的配置格式。

方法

Deserialize(Stream)

還原序列化在提供的資料流上的資料,並重新組合物件 Graph。

Deserialize(Stream, HeaderHandler)

將資料流還原序列化至物件圖形中,使用指定的 HeaderHandler 處理該資料流中的任何標頭。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Serialize(Stream, Object)

使用指定的根,將物件或物件的圖形序列化至指定的 Stream

Serialize(Stream, Object, Header[])

使用指定的根,將物件或物件圖形序列化至 SOAP 遠端程序呼叫 (RPC) 格式的指定 Stream

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於