How to: Serialize an Object as a SOAP-Encoded XML Stream
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
Create the class using the XML Schema Definition Tool (Xsd.exe).
Apply one or more of the special attributes found in
System.Xml.Serialization
. See the list in "Attributes That Control Encoded SOAP Serialization."Create an
XmlTypeMapping
by creating a newSoapReflectionImporter
, and invoking theImportTypeMapping
method with the type of the serialized class.The following code example calls the
ImportTypeMapping
method of theSoapReflectionImporter
class to create anXmlTypeMapping
.' 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));
Create an instance of the
XmlSerializer
class by passing theXmlTypeMapping
to the XmlSerializer(XmlTypeMapping) constructor.Dim mySerializer As XmlSerializer = New XmlSerializer(myTypeMapping)
XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);
Call the
Serialize
orDeserialize
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);