Getting Started with XML Serialization

excel to xmlSerialization is a process of converting an object into some serial data like Extensible Markup Language (XML) or Binary format that is easily shared across different technologies and kept persistent in a storage medium. This serialized data is then transported over the Internet using ‘HTTP’. On the other hand, ‘Deserialization’ is a process of reverting back serialized data to an object form. Object serialization is natively supported by several object-oriented programming languages including .NET framework languages, Objective C, Java, Perl, Python, PHP, Ruby and C++.

Learn more about XML with this Udemy.com. course

What is XML Serialization?

XML serialization is a process in which an object is converted to XML format, which is human-readable text based data. XML is one of the simplest ways to represent complex information. Since XML is open-standard, it is frequently used for communication across systems regardless of the programming language involved. It’s also supported natively in several languages. If it isn’t supported, third-party libraries exist for almost every programming language that lacks XML serialization.

How XML Serialization Works

Before implementation of XML serialization, here are some things you should keep in mind.

  • XML serialization serializes only public fields and properties of an object and parameters and returned values of methods into the XML stream.
  • Deserializing the serialized object does not guarantee that both original and deserialized objects will have the same type, because XML serialization does not contain any data type information.
  • XML serialization is not able to serialize private and protected members, read-only properties, and methods of a class.
  • Names of a class and members are the same in the XML file exactly as they are in the class declaration.

Next, we’ll focus on the basic implementation of ‘XML Serialization’ in the two most commonly used object-oriented languages, .NET C# and Java.

XML Serialization in .NET C#

The class used for ‘XML Serialization’ in .NET framework is ‘XmlSerializer’ located in the ‘System.Xml.Serialization’ namespace. The ‘Serialize’ and ‘Deserialize’ methods of class ‘XmlSerializer’ are the important ones since they are used to serialize and deserialize a data respectively. The following code snippets illustrate the use of ‘Serialize’ and ‘Deserialize’ methods.

Code snippet (Book Class to serialize and deserialize):

 public class Book
{

public int ID { get; set; }

public string Name { get; set; }

public string Version { get; set; }

public int YearOfPublish { get; set; }

}
Code snippet:
static void Main(string[] args)

{

Book singleBook = new Book ();

singleBook.ID = 1;

singleBook.Name = "XML Serialization in .NET Framework";

singleBook.Version = "1.2";

singleBook.YearOfPublish = 2014;

 

XMLSerialize (singleBook);

 

Book deserialized_Book_Object = XMLDeSerialize ();

Console.WriteLine ("Name: " + deserialized_Book_Object.Name);

Console.WriteLine ("Version: " + deserialized_Book_Object.Version);

Console.WriteLine ("Publishing Year: " + deserialized_Book_Object.YearOfPublish);

Console.ReadKey ();

}

static public void XMLSerialize (Book singleBook)

{

XmlSerializer serializer = new XmlSerializer (typeof (Book));

using ( TextWriter writer = new StreamWriter (@"D:\Book.xml"))

{

serializer.Serialize (writer, singleBook);

}

}

static public Book XMLDeSerialize ()

{

XmlSerializer deserializer = new XmlSerializer (typeof (Book));

object obj;

using ( TextReader reader = new StreamReader (@"D:\Book.xml"))

{

obj = deserializer.Deserialize (reader);

}

Book book_Obj = (Book) obj;

return book_Obj;

}

In the above code, an additional ‘System.IO’ classes ‘TextWriter’ and ‘TextReader’ are used to save the XML Serialized data to a storage medium and retrieved it respectively.  The following is an example of the XML Serialized data saved in a file named ‘Book.xml’.

Output:

<?xml version="1.0" encoding="utf-8"?>

<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<ID>1</ID>

<Name>XML Serialization in .NET Framework</Name>

<Version>1.2</Version>

<YearOfPublish>2014</YearOfPublish>

</Book>

From the above output, you can see that names of a class and its members are the same, as they are in the class declaration. To change this default behavior, the following attributes can be used:‘XmlElement ([elementName])’ specifies that the field is serialized as an XML element with an optional argument that is used as an element name instead of a field name in class declaration. The ‘XmlAttribute ([attributeName])’ identifies the field as the attribute in serialization process.

  • ‘XmlElement ([elementName])’ specifies that the field is serialized as an XML element with an optional argument that is used as an element name instead of a field name in class declaration. The ‘XmlAttribute ([attributeName])’ identifies the field as the attribute in serialization process.
  • The ‘XmlRoot’ attribute represents a root element of XML.
  • The ‘XmlIgnore’ attribute tells the program to ignore the field in XML serialization.

The following example shows the use of attributes in a class definition.

Code snippet (Modified definition of the Book class):

[XmlRoot]

public class Book {

[XmlAttribute ("BookID")]

public int ID { get; set; }

[XmlElement ("BookName")]

public string Name { get; set; }

[XmlElement ("BookVersion")]

public string Version { get; set; }

[XmlIgnore]

public int YearOfPublish { get; set; }  }

Output:

 <?xml version="1.0" encoding="utf-8"?>

<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" BookID="1">

  <BookName>XML Serialization in .NET Framework</BookName>

  <BookVersion>1.2</BookVersion>

</Book>

In the above output, you can see that ‘ID’, ‘Name’ and ‘Version’ field names are changed to values defined in the attributes. The ‘ID’ field becomes an attribute to the ‘Book’,  which is the root element.

Interested in learning more about C#? Take this course at Udemy.com.

XML Serialization in Java

In Java, classes used for ‘XML Serialization’ and ‘Deserialization’ are ‘XMLEncoder’ and ‘XMLDecoder’ placed in a ‘java.beans’ package. The Annotations are used to customize the content of XML. The following code snippets demonstrate the use of these classes and annotations on example of a Book class.

Code snippet (Book Class to serialize and deserialize):

@XmlRootElement (name = "Book")

@XmlType (propOrder = {"ID", "name", "version", "yearOfPublish"})

publicclass Book {

@XmlElement (required = true, name = "ID")

publicintID;

@XmlElement (required = true, name = "name")

public String name;

@XmlElement (required = true, name = "version")

public String version;

@XmlElement (required = true, name = "yearOfPublish")

publicintyearOfPublish;

}
Code snippet:

Book newBook = new Book ();
    1. ID = 01;

 

  • name = “XML Serialization in Java”;

 

 

  • version = “1.0”;

 

 

  • yearOfPublish = 2014;

 

 

XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream ( new FileOutputStream (“D:\\Book.xml”))); encoder.writeObject (newBook); encoder.close (); XMLDecoder decoder = new XMLDecoder( new BufferedInputStream( new FileInputStream (“D:\\Book.xml”))); Book deserialized_Book = (Book) decoder.readObject (); decoder.close (); System.out.println (“ID: ” + deserialized_Book.ID); System.out.println (“Name: ” + deserialized_Book.name); System.out.println (“Version: ” + deserialized_Book.version); System.out.println (“Published Year: ” + deserialized_Book.yearOfPublish);

In the above code snippets, a ‘Book’ class with annotations is defined to specify the root element and XML element. A ‘writeObject’ method of ‘XMLEncoder’ type encodes the Book object into XML and saves it on a disk. Finally, a ‘readObject’ method of ‘XMLDecoder’ decodes the XML content into an object and casts it to a Book object.

Want to learn more? Take a tutorial at Udemy.com.