Serialization XML StyleXML serialization enables us to transform an instance of some class to XML and vice versa. Developers often need to perform XML serialization. Using Basic XML SerializationListing 8.12 presents an example of XML serialization. In this example, there is a simple class that needs to be serialized: CellPhone. The first step is to create an instance of the class XmlSerializer and initialize is with the type XMLSerializationSample.CellPhone. Next, the CellPhone class is instantiated and the properties set to the desired values. Finally, the Serialize method is invoked and the CellPhone is serialized. Listing 8.12. XML Serialization Exampleusing System; using System.Xml.Serialization; namespace XMLSerializationSample { public class CellPhone { public CellPhone () { } public string Name { get {return this._name;} set {this._name = value;} } public int Year { get {return this._year;} set {this._year = value;} } public string Description { get {return this._description;} set {this._description = value;} } private string _name = ""; private int _year = 2000; private string _description = ""; } class XMLSerializationSample { [STAThread] static void Main(string[] args) { XmlSerializer serializer = new Xmlizer(Type.GetType("XMLSerializationSample The result of the example's run is shown in Listing 8.13. Listing 8.13. Generated XML Document Based on Listing 8.12<?xml version="1.0" encoding="cp866"?> <CellPhone xmlns:xsd= "http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Name>MC60</Name> <Year>2000</Year> <Description>Some description</Description> </CellPhone> The following lines show us how to deserialize XML by using Deserialize() method: XmlSerializer serializer = new XmlSerializer(Type.GetType("XMLSerializationSample.CellPhone")); CellPhone anotherCellPhone = (CellPhone) serializer.Deserialize(new XmlTextReader("cellphone.xml")); Console.WriteLine(anotherCellPhone.Description); Customizing XML SerializationIn the preceding section, you learned how to serialize an object by using XmlSerialization. This section shows you how to customize this class. For example, suppose that you need to represent a public property of a class not as an XML element, but as an XML attribute. However, the standard serialization process converts public properties into XML elements only. Therefore, you will need to customize the serialization process to accomplish your goal. There are several predefined metadata attributes in .NET that enable us to customize serialization. They control how classes are mapped to XML and contain auxiliary information for Serialize() and Deserialize() methods of the XmlSerializer class. Each attribute customizes how XmlSerializer maps a class, field, or property to an XML document. The attributes can also declare types that are not explicitly referenced in a source file. Now let's tune the source code defined in Listing 8.12 by adding serialization attributes to the class and properties definitions (Listing 8.14). Listing 8.14. Using Attributes for Customizing XML Serializationusing System; using System.Xml.Serialization; namespace XMLSerializationSample { [XmlRoot("cell-phone")] public class CellPhone { public CellPhone () { } [XmlAttribute("name")] public string Name { get {return this._name;} set {this._name = value;} } [XmlAttribute("year")] public int Year { get {return this._year;} set {this._year = value;} } [XmlElement("description")] public string Description { get {return this._description;} set {this._description = value;} } private string _name = ""; private int _year = 2000; private string _description = ""; } class XMLSerializationSample { [STAThread] static void Main(string[] args) { XmlSerializer serializer = new XmlSerializer(Type.GetType("XMLSerializationSample.CellPhone")); CellPhone cellPhone = new CellPhone(); cellPhone.Description = "Some description"; cellPhone.Name = "MC60"; serializer.Serialize(Console.Out, cellPhone); } } } As you can see, the definition of the CellPhone class has changed in the following way:
Listing 8.15 shows the result of running the example described in Listing 8.14. Listing 8.15. XML Generated After Customizing XML Serialization
<?xml version="1.0" encoding="cp866"?>
<cell-phone xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="MC60" year="2000">
<description>Some description</description>
</cell-phone>
![]() |