SoapFormatter 类

定义

以 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
继承
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 开始, 此类已过时。Beginning with the .NET Framework 2.0, this class is obsolete. 请改用 BinaryFormatterUse BinaryFormatter instead.

IFormatter IRemotingFormatter和类实现接口IRemotingFormatter以支持远程过程调用 (rpc) 和接口 (由继承) 以支持对象图形的序列化。 BinaryFormatter SoapFormatterThe 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. 类还支持对对象进行ISoapMessage rpc, 无需使用IRemotingFormatter此功能。 SoapFormatterThe SoapFormatter class also supports RPCs with ISoapMessage objects, without using the IRemotingFormatter functionality.

在 rpc 过程中IRemotingFormatter , 接口允许指定两个单独的对象图: 要序列化的对象的图形, 以及包含一个标头对象数组的附加关系图, 该数组传递有关远程函数调用的信息 (对于例如, transaction 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.

还可以处理通过实现接口的对象生成的 rpc。 ISoapMessage SoapFormatterThe SoapFormatter can also handle RPCs that are produced with objects that implement the ISoapMessage interface. 若要在不使用该功能IRemotingFormatter的情况下创建 RPC, 请在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. 若要对以这种方式创建的TopObject RPC 进行反序列化, 则必须将属性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

根据 ISO 8601 序列化 TimeSpan 对象: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. 框架中版本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 类的新实例。Initializes a new instance of the SoapFormatter class with default property values.

SoapFormatter(ISurrogateSelector, StreamingContext)

使用指定的 SoapFormatterISurrogateSelector 初始化 StreamingContext 类的新实例。Initializes a new instance of the SoapFormatter class with the specified ISurrogateSelector and StreamingContext.

属性

AssemblyFormat

获取或设置与查找加集有关的反序列化器行为。Gets or sets the behavior of the deserializer with regards to finding and loading assemblies.

Binder

获取或设置 SerializationBinder,它控制将序列化对象绑定到类型的过程。Gets or sets the SerializationBinder that controls the binding of a serialized object to a type.

Context

获取或设置与 StreamingContext 一起使用的 SoapFormatterGets or sets the StreamingContext used with this SoapFormatter.

FilterLevel

获取或设置用于 .NET Framework 远程处理的自动反序列化的 TypeFilterLevelGets or sets the TypeFilterLevel of automatic deserialization for .NET Framework remoting.

SurrogateSelector

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

TopObject

获取或设置将 SOAP 顶级对象反序列化为的 ISoapMessageGets or sets the ISoapMessage into which the SOAP top object is deserialized.

TypeFormat

获取或设置类型说明在序列化流中的布局格式。Gets or sets the format in which type descriptions are laid out in the serialized stream.

方法

Deserialize(Stream)

反序列化所提供流中的数据并重新组成对象图形。Deserializes the data on the provided stream and reconstitutes the graph of objects.

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)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Serialize(Stream, Object)

将对象或具有指定根的对象图形序列化为给定的 StreamSerializes an object or graph of objects with the specified root to the given Stream.

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()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于