HOW TO:將物件序列化為 SOAP 編碼的 XML 資料流How to: Serialize an Object as a SOAP-Encoded XML Stream

因為 SOAP 訊息是使用 XML 建置的,所以 XmlSerializer 類別可用來序列化類別並產生編碼的 SOAP 訊息。Because a SOAP message is built using XML, the XmlSerializer class can be used to serialize classes and generate encoded SOAP messages. 產生的 XML 會與全球資訊網協會之<Simple Object Access Protocol (SOAP) 1.1>文件中的第 5 節相符。The resulting XML conforms to section 5 of the World Wide Web Consortium document "Simple Object Access Protocol (SOAP) 1.1". 當您建立透過 SOAP 訊息溝通的 XML Web 服務時,您可以將特殊 SOAP 屬性集套用至類別與類別成員以自訂 XML 資料流。When you are creating an XML Web service that communicates through SOAP messages, you can customize the XML stream by applying a set of special SOAP attributes to classes and members of classes. 如需屬性的清單,請參閱控制編碼 SOAP 序列化的屬性For a list of attributes, see Attributes That Control Encoded SOAP Serialization.

將物件序列化為 SOAP 編碼的 XML 資料流To serialize an object as a SOAP-encoded XML stream

  1. 使用 XML 結構描述定義工具 (Xsd.exe)建立類別。Create the class using the XML Schema Definition Tool (Xsd.exe).

  2. 套用在 System.Xml.Serialization中發現的一個或多個特殊屬性。Apply one or more of the special attributes found in System.Xml.Serialization. 請參閱<控制編碼 SOAP 序列化的屬性>中的清單。See the list in "Attributes That Control Encoded SOAP Serialization."

  3. 藉由建立新的 XmlTypeMapping 及使用已序列化類別的型別叫用 SoapReflectionImporter 方法,來建立 ImportTypeMappingCreate an XmlTypeMapping by creating a new SoapReflectionImporter, and invoking the ImportTypeMapping method with the type of the serialized class.

    下列程式碼範例會呼叫 SoapReflectionImporter 類別的 ImportTypeMapping 方法,以建立 XmlTypeMappingThe following code example calls the ImportTypeMapping method of the SoapReflectionImporter class to create an XmlTypeMapping.

    ' Serializes a class named Group as a SOAP message.  
    Dim myTypeMapping As XmlTypeMapping =
        New SoapReflectionImporter().ImportTypeMapping(GetType(Group))  
    
    // Serializes a class named Group as a SOAP message.  
    XmlTypeMapping myTypeMapping =
        new SoapReflectionImporter().ImportTypeMapping(typeof(Group));
    
  4. 利用傳遞 XmlSerializerXmlTypeMapping 建構函式的方式,來建立 XmlSerializer(XmlTypeMapping) 類別的執行個體。Create an instance of the XmlSerializer class by passing the XmlTypeMapping to the XmlSerializer(XmlTypeMapping) constructor.

    Dim mySerializer As XmlSerializer = New XmlSerializer(myTypeMapping)  
    
    XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);  
    
  5. 呼叫 SerializeDeserialize 方法。Call the Serialize or Deserialize method.

範例Example

' Serializes a class named Group as a SOAP message.  
Dim myTypeMapping As XmlTypeMapping =
    New SoapReflectionImporter().ImportTypeMapping(GetType(Group))
Dim mySerializer As XmlSerializer = New XmlSerializer(myTypeMapping)  
// Serializes a class named Group as a SOAP message.  
XmlTypeMapping myTypeMapping =
    new SoapReflectionImporter().ImportTypeMapping(typeof(Group));
XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);  

另請參閱See also