Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

RecentComments

Comment RSS

XML Data Storage and XmlSerializer : The easy data store

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:

[code:c#] 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());
            xmlWriter.Close();
            memStream.Close();
        } [/code]

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:

[code:c#] 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);
            xmlReader.Close();
            stringReader.Close();
            return obj;
        } [/code]

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.

Posted: Mar 24 2011, 10:01 by Admin | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Category: XML | C#
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading