Hi all.

Today I read a topic related to XML, Serializing and Deserializing object, I enjoyed it because it provides an awesome way to convert objects (like classes) to XML. Before today, I used for loops and tags to build XML file, which needs alot of time and takes much of work when I need to modify the XML.

In this post, I will make a simple example to introduce this issue, I’ll use C#, if you need any help in VB.NET please send to me.

I will not talk about XML, I suppose you know whats the benefits and drawbacks of XML, but I will mention two important things when you need to serialize class to XML:

  • XML serialization can serialize only public data. You cannot serialize private data
  • You cannot serialize object graphs; you can use XML serialization only on objects.

The following example is about Person class with some public variables, one of the is an enumerator; Gender:

 

public enum Gender { Male,Female}

To create a serilizable class you must add Serializable attribute for the class, and use an empty constructor, below is the class for Person:

 [Serializable]
public class Person
{
   public string Name;
  
public string Address;
  
public Gender Gender;
  
public DateTime RegistrationDate;
  
public double Mark;

   public Person()
     
{
      }
}
 Now we will create serialize function, which saves the values of Person class object to XML file (Not that we are woking on ASP.NET – C#)
//Create an instance of Person class
Person person = new Person
(); 
//Create File using FileStream
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(“~/SerializedObject.XML”),System.IO.FileMode.Create); 

 

 
//Initialize it
person.Name = “Ibrahim”;
person.Gender =
Gender.Male;
person.Address =
“Palestine”;
person.RegistrationDate =
new DateTime(2009, 1, 1);
person.Mark = 94.5;

 

 
//Create an XmlSerializer object to perform the serialization
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(Person
)); 
 
//Use the XmlSerializer object to serialize the data to the file 
xs.Serialize(fs, person); 
 
//Close the file
fs.Close();

 

The output XML is like this:

<?xml version=”1.0″ ?>
<Person xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=”http://www.w3.org/2001/XMLSchema>
  <Name>Ibrahim</Name>
  <Address>Palestine</Address>
  <Gender>Male</Gender>
  <RegistrationDate>2009-01-01T00:00:00</RegistrationDate>
  <Mark>94.5</Mark>
</Person>

 

 The deserialization is very similar:

 //Create an of Person class
Person person;

// Create Filestream object to read the data from
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(“~/SerializedObject.XML”), System.IO.FileMode.Open); 
 
  //Create an XmlSerializer object to perform the deserialization
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(Person)); 
  

// Use the XmlSerializer object to deserialize the data from the file
person = (Person)xs.Deserialize(fs); 

 //Close the file
fs.Close();

 

Tha’s all… for more details you may read this or this topic from MSDN.

Thanks for reading.