XML Data Storage and XmlSerializer : The easy data store

Thursday, March 24, 2011

Earlier I worked in an application where the data storage was XML and we spent quite some time designing the XSD and finalising what tags will be there for storing the different data. This is quite trivial when we need to use it as a datastore and the application need to be able to get some specific data out of it or we need some query implementations on top of this data storage.

Sometime back, in a team that I was associated with, needed a small datastore for some hierarchical data. The requirement was to get the data loaded into the application right at the startup of the application and save back the changes done at the end of the application or whenever the user wish to save back.

With all natural instincts, the team started thinking of the structure of the XML and ways and means of loading the data from XML and saving the data back node by node. After some hit and trial with some POCs they reported the issue that it is becoming hard to design this structure and writing a good data access or serialisation class.

I think almost any programmer, who has not worked on XML extensively,  tend to start in this manner only. Then with some throw of light on the XmlSerializer and a slightly different angle of thought, the easy and simple solution came up. We used a .NET Collection and other data structures to create our data structure that was easy for the program. For the first time, we set some default value to the structure and simply saved it to an XML file using the XmlSerializer. The method was something like:

public void SerializeXML(object Obj, System.Type ObjType)
            XmlSerializer ser;
            ser = new XmlSerializer(ObjType, TargetNamespace);
            MemoryStream memStream;
            memStream = new MemoryStream();
            XmlTextWriter xmlWriter;
            xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            xmlWriter.Namespaces = true;
            ser.Serialize(xmlWriter, Obj, GetNamespaces());

Voila! We have the XML file without putting any effort on the design of the XML. Thanks to XmlSerializer. Our next point was to prove that we can reuse this XML. So we needed to deserialise the XML file and get back the original data structure. So the deserialisation code:

public object DeserializeFromXML(string xmlString, System.Type ObjType)
            XmlSerializer ser;
            ser = new XmlSerializer(ObjType, TargetNamespace);
            StringReader stringReader;
            stringReader = new StringReader(xmlString);
            XmlTextReader xmlReader;
            xmlReader = new XmlTextReader(stringReader);
            object obj;
            obj = ser.Deserialize(xmlReader);
            return obj;

Awesome! It worked without any glitch and the module was ready in flat 16 hours as the team had to put their effort on the business rules and UI only.

Filed Under: XML, C#

Comments (1) -

Nuno Rocha de SousaBrazilNuno Rocha de Sousa said:

When I read this post I saw all my way in solving a problem like this. First I thought in a structure for all classes to add on XML file Smile
Nice post, not only for the technical issue but also the line of reasoning.

Add comment