Serialization XML Style


XML 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 Serialization

Listing 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 Example

[View full width]

 using 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 .CellPhone"));        CellPhone cellPhone = new CellPhone();        cellPhone.Description = "Some description";        cellPhone.Name = "MC60";        serializer.Serialize(Console.Out, cellPhone);     }   } } 

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 Serialization

In 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 Serialization
 using 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:

  • The XmlRoot attribute was added ([XmlRoot("cell-phone")]) before the class declaration; now the root element of XML will be cell-phone.

  • The XmlAttribute attributes were added before the Name and Year properties ([XmlAttribute("name")], [XmlAttribute("year")]). They will be defined in the XML as attributes of the cell-phone node.

  • The Description property has been declared with the attribute XmlElement ([XmlElement("description")]). In the resulting XML document, the description node will be presented inside the cell-phone node.

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> 



    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net