DekGenius.com
[ Team LiB ] Previous Section Next Section

Chapter 18. The System.Xml.Serialization Namespace

The System.Xml.Serialization namespace contains classes that are used to control the serialization of .NET types to XML. Serialization refers to the process of encoding an object as a series of bytes, which can then be decoded by another program in order to replicate the original object. The System.Xml.Serialization namespace supports XML serialization, the serialization of objects to arbitrary XML formats, including SOAP. For serialization to binary and user-defined formats, as well as to specific SOAP formats used for the transmission of objects between instances of .NET applications, collectively known as runtime serialization, see the System.Runtime.Serialization namespace.

SOAP, which formerly stood for Simple Object Access Protocol, defines a standard mechanism for encoding objects as XML. The specification for SOAP 1.1 is available at http://www.w3.org/TR/SOAP. For more information about SOAP, see Programming Web Services with SOAP, by Doug Tidwell, James Snell, and Pavel Kulchenko (O'Reilly). SOAP Version 1.2 recently became a W3C Recommendation.

To serialize an object instance to XML, create an instance of XmlSerializer for that type by passing the type as a parameter to XmlSerializer's constructor. Then use the XmlSerializer.Serialize( ) method to serialize the object to a System.IO.Stream, System.IO.TextWriter, or System.Xml.XmlWriter. To deserialize an object from XML, use the staticXmlSerializer.Deserialize( ) method, which returns an instance of System.Object that you can cast to the appropriate type. XmlSerializer.Deserialize( ) also allows you to read XML from a System.IO.Stream, System.IO.TextReader, or System.Xml.XmlReader.

To serialize an object instance to SOAP, get an instance of XmlTypeMapping by constructing a new SoapReflectionImporter and calling its SoapReflectionImporter.ImportTypeMapping( ) method with the System.Type you want to serialize. The details of how an object is serialized to XML and SOAP, including the names of elements and attributes and the handling of nested elements, can be specified either by attaching attributes to types and fields in the source code, or at runtime by adding attributes to a XmlAttributeOverrides or SoapAttributeOverrides object, which can be passed to the XmlSerializer or SoapReflectionImporter constructor, respectively.

This namespace contains numerous classes that, although public, are reserved for internal use by the .NET Framework. No documentation is included in this quick reference for those classes. Figure 18-1, Figure 18-2, and Figure 18-3 show the types in this namespace.

Figure 18-1. The System.Xml.Serialization namespace
figs/dnxm_1801.gif
Figure 18-2. Attributes from the System.Xml.Serialization namespace
figs/dnxm_1802.gif
Figure 18-3. Delegates, event arguments, and collections from the System.Xml.Serialization namespace
figs/dnxm_1803.gif
    [ Team LiB ] Previous Section Next Section