As its name suggests, XML serialization is the process of serializing an object into XML format. Quite luckily, the format of the XML file can be massively customized, so be prepared for dumb questions, like which XML file well be the output of this code, or the reverse (even better).
The classes of XML serialization lives in the System.Xml.Serialization namespace. No reference needed, because this namespace is part of the System.Xml.dll, which is linked by Visual Studio for every project.
The main class here is XmlSerializer. Now this class has absolutely nothing to do with the previous formatter classes. It has three useful instance methods, namely Serialize, Deserialize and CanSerialize. Yes, CanSerialize needs an XmlReader and returns true if the read XML file can be serialized by the instance. The Deserialize method is essentially the same as it was at the formatter classes, can read any kind of Stream, or Reader classes (XmlReader included!). However, the Serialize method is very different from the others. It takes a type (only include a single type when you are working with an Object-derived type with no is-a, has-a relationships), and an array of types, for the respective is-a, has-a relationships. Yes, you need to include every single type that participates in your object’s life.
The good news is that you don’t need to include the Serializable attribute (but you should, for the sake of the other formatters). The bad news is that there are a whole lot of new attributes. The most important ones of them:
|Attributes of XML Serialization|
|XmlArray||The members of the array will be generated as the members of an XML array.|
|XmlArrayItem||Derived types that can be inserted to the array.|
|XmlAttribute||The member will be serialized as an XML attribute.|
|XmlElement||The member will be serialized as an XML element.|
|XmlEnum||The element name of an enumeration member.|
|XmlIgnore||The marked member won’t be participating in the XML serialization.|
|XmlInclude||The class should be included when generating schemas.|
|XmlRoot||Controls the namespace and root name of the XML document.|
|XmlText||The property will be generated as XML text.|
If the functionality provided by these attributes isn’t enough for you, you can implement the IXmlSerializable interface. It has the following methods:
- void ReadXml(XmlReader): let’s you read the generated XML to control the deserialization process massively.
- void WriteXml(XmlWriter): fine-tune the XML serialization process with this method.
- XmlSchema GetSchema(): let’s you work with the generated schema.
XmlSerializer has some useful events that you might like. These events occur during deserialization and are as follows:
- UnknownAttribute, UnknownNode, UnknownElement: when the unknown thing is found during deserialization, these events will fire.
- UnreferencedObject: occurs during the deserialization of SOAP-encoded XML stream, when XmlSerializer stumbles into a recognized typed that is unreferenced or unused.