How to serialize an object using an ISerializable interface

Introduction

Some times, we need to record data according to a given object into a file in order to either stock them or to send them via network, and such task are very needed especially in the asp or the distributed applications case. This kind of task is called serialization. There are several methods to do that, but never the less, I give one alternative among them as a part of this article. This method consists on implementing the ISerializable interface. 

Let's consider a class who's called Person and here is its description:

Figure 1

The Person class code is as follow:

public class Person
{
    private string _FirstName;
    private string _LastName;
    public string FirstName
    {
        get { return _FirstName; }
        set { _FirstName = value; }
    }
    public string LastName
    {
        get { return _LastName; }
        set { _LastName = value; }
    }
    public Person()
    {
    }
    public Person(string FirstName, string LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
    }
}

To serialize objects issued from this class, we should implement the ISerializable interface; this last one has a method ISerializable.GetObjectData that it should be implemented. 

This is how to implement the ISerializable interface:

public class Person:ISerializable
{
    private string _FirstName;
    private string _LastName;
    public string FirstName
    {
        get { return _FirstName; }
        set { _FirstName = value; }
    }
    public string LastName
    {
        get { return _LastName; }
        set { _LastName = value; }
    }
    public Person()
    {
    }
    public Person(string FirstName, string LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
    }
    void ISerializable.GetObjectData(SerializationInfo oInfo, StreamingContext oContext)
    {
        oInfo.AddValue("FirstName", this.FirstName);
        oInfo.AddValue("LastName", this.LastName);
    }
}

The GetObjectData ( ) method hasn't to be marked as public otherwise an error indicates that the modifier public in not valid for this item will be generated.

Figure 2

Now to serialize an object, we create a user interface as bellow:

Figure 3

As the figure above shows, add three labels and change their text properties, respectively, to Person, First name and Last name. After that, add to text boxes and name them respectively txtFirstName and txtLastName. Finally, add two buttons and name them btnCancel and btnSerialize then change their text properties respectively to Cancel and Serialize. Now, implement the Serialize button event handler as follow:

private void btnSerialize_Click(object sender, EventArgs e)
{
    try
    {
        Person oPerson = new Person();
        oPerson.FirstName = txtFirstName.Text;
        oPerson.LastName = txtLastName.Text;
        XmlSerializer oSerialiser = new XmlSerializer(typeof(Person));
        Stream oStream = new FileStream(@"C:\xmlFile.xml", FileMode.Create);
        oSerialiser.Serialize(oStream, oPerson);
        oStream.Close();
        MessageBox.Show("The job is done");
    }
    catch (ApplicationException caught)
    {
        MessageBox.Show(caught.Source); 
    }
}

And implement the cancel button event handler as follow:

private void btnCancel_Click(object sender, EventArgs e)
{
    txtFirstName.Text = "";
    txtLastName.Text = "";
}

Now, fire up the application, add a first then a last name and press the Serialize button.

Figure 4

And the job will be done:

Figure 5

Now, browse to the "C:\" directory, you find there a file with "xmlFile.xml" as a name. And here the file contents:

<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Bejaoui</FirstName>
  <LastName>Bechir</LastName>
</Person>

 


Similar Articles