How to: Serialize an Object as a SOAP-Encoded XML Stream - .NET (2024)

Because a SOAP message is built using XML, the XmlSerializer class can be used to serialize classes and generate encoded SOAP messages. The resulting XML conforms to section 5 of the World Wide Web Consortium document "Simple Object Access Protocol (SOAP) 1.1". 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. For a list of attributes, see Attributes That Control Encoded SOAP Serialization.

To serialize an object as a SOAP-encoded XML stream

  1. Create the class using the XML Schema Definition Tool (Xsd.exe).

  2. Apply one or more of the special attributes found in System.Xml.Serialization. See the list in "Attributes That Control Encoded SOAP Serialization."

  3. Create an XmlTypeMapping by creating a new SoapReflectionImporter, and invoking the ImportTypeMapping method with the type of the serialized class.

    The 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. 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. 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

How to: Serialize an Object as a SOAP-Encoded XML Stream - .NET (2024)

References

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6275

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.