Object to XML Serialization in .NET

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:

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 =
person.Address =
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


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>


 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


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

Thanks for reading.

One thought on “Object to XML Serialization in .NET

  • Baha' Ashour
    16/08/2009 at 9:32 AM

    Really cool! I like that

Leave a Reply

Your email address will not be published. Required fields are marked *.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.