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接口允许两个单独的对象关系图的规范: 要序列化的对象图和额外的关系图,其中包含传递识别信息 (适用于远程函数调用的标头对象的数组例如,事务 ID 或方法签名)。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 Serialization

根据 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

获取或设置 SerializationBinder,它控制将序列化对象绑定到类型的过程。Gets 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

获取或设置 SurrogateSelector,它控制序列化和反序列化过程中的类型替换。Gets or sets the SurrogateSelector that controls type substitution during serialization and deserialization.

TopObject TopObject TopObject TopObject

获取或设置将 SOAP 顶级对象反序列化为的 ISoapMessageGets 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)

反序列化所提供流中的数据并重新组成对象图形。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 的浅表副本。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)

适用于