The XmlSerializer Class
The central element in the XML serialization architecture is the XmlSerializer class, which belongs to the System.Xml.Serialization namespace. The XML serialization process can be used to quickly develop an XML-driven application and get the best of both worlds—objects for programming and XML for data transfer or storage. The XmlSerializer does the transformations from one representation to the other. To accomplish its functionalities, the XmlSerializer class exposes the methods shown in Table 12-1.
|
Method |
Description |
|
CanDeserialize |
Indicates if the XmlSerializer object can deserialize a specified XML document |
|
Deserialize |
Deserializes an XML document that is read from a stream, text, or an XML reader |
|
FromMappings |
Static method that returns an instance of the XmlSerializer class from the specified mappings that are created through the XmlTypeMapping object |
|
FromTypes |
Static method that returns an array of XmlSerializer objects created from an array of types; useful for speeding operations when you need to create multiple serializers for different types |
|
GenerateSerializer |
Static method that returns an XML serialization assembly that contains typed serializers; the returned assembly is one that is created through the tool XML serializer generator (sgen.exe) |
|
GetXmlSerializerAssemblyName |
Static method that returns the name of the assembly that contains one or more versions of the XmlSerializer that is specifically created to serialize or deserialize specific types |
|
Serialize |
Serializes an object into an XML document |
The Serialize() method has a number of overloads. The first parameter to the Serialize method is overridden so that it can serialize XML to a Stream, a TextWriter, or an XmlWriter. When serializing to Stream, TextWriter, or XmlWriter a third parameter to the Serialize method is permissible. This third parameter is of type XmlSerializerNamespaces and is used to specify a list of namespaces that qualify the names in the XML-generated document. The permissible overrides of the Serialize method are:
void Serialize(Stream, Object); void Serialize(TextWriter, Object); void Serialize(XmlWriter, Object);
void Serialize(Stream, Object, XmlSerializerNamespaces);
void Serialize(TextWriter, Object, XmlSerializerNamespaces);
void Serialize(XmlWriter, Object, XmlSerializerNamespaces);
void Serialize (XmlWriter, Object, XmlSerializerNamespaces, String)
void Serialize (XmlWriter, Object, XmlSerializerNamespaces, String, String)
The fourth and fifth parameters allow you to specify the encoding style and the id used for generating SOAP encoded messages respectively. Now that you have a general understanding of the XmlSerializer class, the next section discusses an example that exercises these concepts.
Post a comment