How a Custom Xml Serializable Class Is Created by Using IXml Serializable

IXmlSerializable interface provides custom formatting for XML serialization and deserialization f \ The methods that control serialization and deserialization in the

IXmlSerializable interface are:

SetSchema

WriteXml

ReadXml

XML

r*

The IXmlSerializable interface provides custom formatting for XML serialization and deserialization. The XmlSerializer class implements the IXmlSerializable interface, and the custom classes use it to control how they will be serialized or deserialized through the XmlSerializer class. For a finer granularity of control on the code in custom classes, and a tighter direction of output, you should implement the IXmlSerializable interface in custom classes. You also obtain control of the XML file when serializing or deserializing objects at run time. As a result, by using the IXmlSerializable interface, you can control the way objects are serialized into XML format. However, you cannot control the serialization of objects into XML format if you use the XmlSerializer class without implementing the IXmlSerializable interface. In addition, the implementation of the IXmlSerializable interface helps you chunk the serialized XML data in a better way.

The following table lists the methods that control serialization and deserialization in the IXmlSerializable interface.

Method

Description

GetSchema

The GetSchema method works in conjunction with XmlSchemaProviderAttribute, which is available in the System.Xml.Serialization namespace and helps serialize and control the XML schema.

WriteXml

The WriteXml method is applied during serialization when the XmlSerializer detects that an object implements the IXmlSerializable interface. The WriteXml method converts an object into its XML representation. You need to write the required information to the XmlWriter stream to allow the ReadXml method to reconstitute objects.

ReadXml

The ReadXml method is applied during deserialization after the method is able to identify a type that implements the IXmlSerializable interface in the XML stream. The ReadXml method reconstitutes an object by using the information that was written by the WriteXml method. The ReadXml method generates an object from its XML representation. When you call this method, the reader is positioned at exactly the same point where the WriteXml method had started writing. When this method returns, it reads exactly those nodes that were written by the WriteXml method.

The following code sample shows how to implement the IXmlSerializable interface and its methods. The code example implements the IXmlSerializable interface in a class named Temperature and defines the ReadXml method to read data during deserialization. The code also defines the WriteXml method to write data during serialization. The Temperature class provides an equivalent reference type to represent a particular temperature. The class provides a property named Degree to represent the value for degree of the current Temperature object.

Implementing the WriteXml method writes the degree value of the Temperature object into the XML stream as a string, and implementing the ReadXml method reads the degree value back from the XML stream and stores it into the corresponding field in the object. The Main method shows how to create and serialize an object of the Temperature class to an XML file on the disk. The object is serialized according to the implementation provided through the WriteXml method in the Temperature class. Finally, the code creates a second object of the Temperature class by deserializing the data in the XML file and displays the newly created object in the console. The data in the XML file is deserialized according to the implementation provided for the ReadXml method in the Temperature class.

Visual Basic

Imports System

Imports System.IO

Imports System.Xml

Imports System.Text

Imports System.Xml.Serialization

Public Class Temperature

Implements IXmlSerializable Private degreeOf As Single

Public Shared Sub Main() Try

Dim tw As New StreamWriter("C:\Temperature.xml")

Dim s As New XmlSerializer(GetType(Temperature))

Dim t1 As New Temperature()

t1.Degree = 39.1C

s.Serialize(tw, t1)

tw.Close()

Dim fs As New FileStream("C:\Temperature.xml", FileMode.Open) Dim t2 As Temperature t2 = CType(s.Deserialize(fs), Temperature) Console.WriteLine(t2) fs.Close() Catch ex As Exception

Console.WriteLine(ex.ToString()) Finally

Console.WriteLine()

Console.Write("Press ENTER to exit...") Console .ReadLine () End Try End Sub End Class

using System;

using System.IO;

using System.Xml.Serialization;

public class Temperature : IXmlSerializable {

private float degreeOf;

StreamWriter tw = new StreamWriter("C:\\Temperature.xm1");

XmlSerializer s = new Xm1Seria1izer(typeof(Temperature));

Temperature t1 = new Temperature();

s.Seria1ize(tw, tl);

FileStream fs = new Fi1eStream("C:\\Temperature.xm1", FileMode.Open); Temperature t2;

Conso1e.WriteLine(t2);

catch (Exception ex)

Console.WriteLine(ex.ToStringQ);

finally {

Console.WriteLine() ; Console.Write("Press ENTER to exit.. Console .ReadLine ();

Was this article helpful?

0 0

Post a comment