XML Serializer Class for Reading and Writing XML

There are many ways to read and write XML.

The advantage of the XmlSerializer class is that you can read and/or write XML with very little code. Most of the code required is simply the definition of the data. In other words, if our data is a list of Links consisting of a HREF or URL, a title and a category, then that data could be defined in the following manner:

class LinkObject
      string ThisCategory;
      string ThisHRef;
      string ThisTitle;

      public string Category
            get { return ThisCategory; }
            set { ThisCategory = value; }

      public string HRef
            get { return ThisHRef; }
            set { ThisHRef = value; }

      public string Title
            get { return ThisTitle; }
            set { ThisTitle = value; }

Using the XmlSerializer class, we use Serialize.Deserialize to read the data and XmlSerializer.Serialize to write the data. An instance of the XmlSerializer class could be created using:

Serializer = new XmlSerializer(typeof(LinkObjectsList));

Then the data could be written using:

Writer = new StreamWriter(Filename);
Serializer.Serialize(Writer, LinksList);

Data could be read using:

Reader = new StreamReader(Filename);
LinksList = (LinkObjectsList)Serializer.Deserialize(Reader);

It is nearly that easy. Note that when the data is as simple of the above data, it is possible to read and write the data using a DataTable. If however the data is more complicated than what a single DataTable is capable of, then the XmlSerializer class can be easier (see below).

Note that the LinkObject class above represents one link. We are writing and reading a list of links, where list could be called an array or a collection or a table or something else. We can create a list of links using:

<LinkObject> LinksList = new List<LinkObject>();

If we do that, then in the XML the list of Link items will have the element name "ArrayOfLink" by default (that is how XmlSerializer works). If we want the element name to be something else, such as "Links", then we need to use an attribute (A C# attribute) to specify the name we want to be used. The XmlRoot attribute can be used except it can only be used with a class, structure, enumeration, or interface; it cannot be used with a declaration of an instance of an object. So the following shows how to create a class for our list of LinkObject items:


class LinkObjectsList : List<LinkObject> { }

Note that all items to be serialized and/or deserialized must have Public access.

A XmlType attribute can be used to specify that each LinkObject item is to be serialized and deserialized as a "Link" object; in other words, to use Link as the XML element name instead of LinkObject.

Fields (members of the class to be serialized and deserialized) are attributes of the element that contains them if the "XmlAttribute" is used for the item in the class.

More Complex Data

Members of the class to be serialized and deserialized can be an instance of another class or a list or array or other collection. This is where the power of the XmlSerializer class is, since you can read and write complex data simply by writing the data definitions. Use the XmlElement attribute to override the name used in the XML for items in a class.