XML serialization of objects in C# and Java

Posted by | October 24, 2010 | C#, Java, XML | No Comments

In this post I will explain some basic methods for creating XML serialization of objects in C# and Java but before let’s take a look and see what actually XML is and when should we use it.
XML or eXtended Markup Language is as it says his name nothing more the a markup language same as HTML, but the main difference beside the possibility of creating you’re own tags, is that XML is used to structure information. The posibility of structuring information made from XML a really useful tool to transport data across application that can even reside on different machines across internet, here we can remember the REST  and OData protocol. So  XML can be used to expose and access information from a variety of sources including, but not limited to, relational databases, file systems, content management systems and traditional Web sites.

Now that we know what is XML good for is time to get in to the business and write out first class that cam be serialized as XML.

C#

using System.Xml.Serialization;
[XmlRoot(ElementName = "PersonalInformations")]

public class PersonalInformations
{
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }

[XmlAttribute(AttributeName="age")]
public uint Age { get; set; }
[XmlAttribute(AttributeName="birth")]
public DateTime DateOfBirth { get; set; }

public string Adress{get;set;}

public static void StoreObject(Stream outStream, PersonalInformations obj)
{
XmlSerializer x = new XmlSerializer(typeof(PersonalInformations));
x.Serialize(outStream, obj);
}
}
public static PersonalInformations LoadObject(Stream inStream)
{
XmlSerializer x = new XmlSerializer(typeof(PersonalInformations));
return  x.Deserialize(inStream) as PersonalInformations;
}
}

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Root(name="PersonalInformations")
class PersonalInformations {
 
@Attribute(name="name")
private String name;
 
@Attribute(name="age")
private int age;
 
@Attribute(name="birth")
private Date dateOfBirth;
 
@Element(name="birth")
private Date dateOfBirth;
 
public static void storeObject(Stream outStream, PersonalInformations obj)
 
{
      Serializer serializer = new Persister();
      serializer.write(obj, outStream);
}
 
}
 
public static PersonalInformations loadObject(Stream inStream)
 
{
      Serializer serializer = new Persister();
      return (PersonalInformations) serializer.read (PersonalInformations.class,inStream);
}
 
}

In the case of C# we made use of the classes from System.Xml.Serialization namespace, that offers a simple way to serialize classes. In case of Java we use a third part library that can be downloaded from http://simple.sourceforge.net. In both cases we annote the class fields to inform the serialization service about the way we want to create the XML.
As you maybe have already noticed from this sample, XML has become more then a way to store structured information, it is actually a way to express an object state. State that can be stored or sent across applications, applications that can be write in different programming languages.

The goal of this post is not informing about how to serialize classes to XML but to show the simplicity and beauty of XML in communication between applications.

About Andrei Tara

Andrei is a developer/geek who loves creating high quality and efficient applications, always following the best practices in software engineering. He loves the challenge of learning new, exciting things and playing with new languages, frameworks, and tools.

Leave a Reply