Monday, August 25, 2008

XML Serialization




Nice article about how to Serialize and DeSerialize objects in XML.http://www.dotnetfunda.com/articles/article98.aspx. This is really handy when you want to make saving objects very versatile and not database dependent. Below is a list of additional reasons to use XML Serialization.

  • Storing user preferences in an object.
  • Maintaining security information across pages and applications.
  • Modification of XML documents without using the DOM.
  • Passing an object from one application to another.
  • Passing an object from one domain to another.
  • Passing an object through a firewall as an XML string.
  • Source (http://www.dotnetjohn.com/articles.aspx?articleid=173)

To make objects serializable, I create an XSD schema that represents my object. You know, contract first, then code last.




Then I use a code generator that will generate a .NET class that makes my object XML serializable. Its called XSD Object Generator which is a plug in for Visual Studio. You can download the tool from here: http://weblogs.asp.net/jdanforth/archive/2004/09/18/231209.aspx.

Then you can create a XML serializer and deserializer helper classes. These classes will take an object in memory and serialize it to an XML stream or hydrate a XML stream to an object. I usually hook in a Database helper class and save the XML string to the database for later use.


Have fun! Let me know if this makes sense.





No comments: