In part one, two and three we looked at how we can take a normal .net object and serialize it out to XML, along the way we also took a look at some of the XML attributes we can apply to our property and class declaration. In this final part of the series we will take a look at how we can read the XML then de-serialize it back into our .net object.

The .net framework makes de-serialising back into a .net object straightforward, in the below example we will de-serialize the employee XML file that we serialized in part three back into the employee class that we also defined in part three, below is a recap on what the XML file looks like.

Now for reading the XML and de-serializing into our employee object, first we create a new instance of a XMLSerializer and tell it what type it will be handling which in this case will be our employee class. Next we create a new file stream object that will open and read our XML file from disk which we will then use in our XMLReader. The last thing we need to do is assign the output from the XMLSerializer and type cast it to employee.

Once the above code has ran the oEmployee object will now hold the de-serliazed XML and the XMLSerialzer has no problem with handling our inherited class or our list of addresses property as you will see from the screen shot below which shows the member information for our object after the reader code has ran,

In part one and part two we looked at the basics of how we can serialize objects to XML. In this part we will take a look at lists and inheritance when serialising objects out to XML.

Serializing Generic Lists

In this example we are going to build upon the person class that we created in part 1 by adding a generic list(of t) property that will hold a list of address information objects. First a quick recap on what is defined in the person class,

So we now have a person object populated with a list of address information and we have serialized this out to XML without needing to make any additional changes to the serializer, below is the XML output stored in our file "MyNewXMLFile.XML",

Serializing Inherited Objects

The XML serializer will also handle serializing inherited classes without the need for any additional code, as an example we will create a new class called employee which will inherit the person class from the example above plus add add three more properties of its own,

Next we need to change our object to serialize from the person to employee class and initialise the additional fields from the employee class, the rest of the code remains the same as in the below code snippet,

In part 1 we looked at how to serialize objects to XML, if you haven’t read part 1 I encourage you to do so as we will build on the examples from that post. In this part we will look at some of the attributes we can add to our class from the System.Xml.Serialization namespace so that we can control how the XML is generated.

The person class is relatively simple with four public properties and when serialized the serialzer will use the property names as the XML element names as we saw in part 1. But what if we want a different XML element name and not the property name from our class, well thats where the XMLElement attribute comes in to play.

As an example we will use the XMLElement attribute to change the FamilyName property to be written to XML as “Surname”, below is the code snippet for this

In the above examle we changed the element name but what if we don’t want one of the public properties to be serialized for this we can use the XmlIgnore attribute, as an example for our person class we don’t want to write the DateOfBirth property out to XML so we can add the XMLIgnore attribute

The XML root element will by default be the class name so in our example the root element is called person, but we can also use an attribute to change how this gets serialized, for example if we wanted to change the root element name to employee we could add an XmlRootAttibute to the class as below

Now when our XML is generated it will have the root element Employee rather than Person. We can also use the XmlRootAttribute to specify a XML namespace an example of this is below, our person class now looks like the following

When the class is serialized the root element will now be Employee rather than Person and have a user defined namespace, the FamilyName property will be changed to Surname and the DateOfBirth property will be ignored and not written to XML, our XML output will now look like

There are a number of ways to generate XML files from within .net but if you need to serialize objects to XML by far the easiest method is to use the System.Xml and System.Xml.Serialization namespaces to do just that, the XML namespaces give us some real powerful attributes and methods which simplify the whole declaration and generation process.

This post is part one of a four part series which explores serializing object to XML, in this part we will take a look at the basics, in part two we will take a look into what we can do with the XML namespace to manipulate our object, in part three we will look at how we can serialize generic lists and embedded objects and then finally in part four we will look at how we can read in the XML files and convert back into a .net object.

Lets look at an example where we want to be able to serialize a “person” object to disk, our person class only has four properties for simplicity and they are all “public”, the class looks like

As you can see our class is just a plain .net class with no mention of anything do with XML. Now we will create a new person object give it some values and then serialise the object using XmlSerializer, below are the required namespaces that we will be using.

Imports System.XmlImports System.Xml.SerializationImports System.IO

Now to serialise and write out the object to XML, first we define our person object and then create a new StreamWriter object which will handle the writing to disk, then lastly we use the XmlSerializer to convert our person object to XML.