This is a mirror of official site: http://jasper-net.blogspot.com/

How to Serialize/Deserialize Complex XML in ASP.Net / C#

| Tuesday, April 6, 2010
Did you ever want to Serialize/Deserialize a complex XML, like Youtube's API XML Response or Flickr's Feed XML Response. Lets start with Flickr's XML Response and see how to Deserialize it.
You can view the full details on XML over here: http://www.flickr.com/services/feeds/docs/photos_public/

To download/View sample XML head over to this link http://api.flickr.com/services/feeds/photos_public.gne?tags=water

Lets start disecting the XML First thing to identify are the Namespaces Used in the Feed, Namesaces
The namespace used here is:

   * xmlns="http://www.w3.org/2005/Atom"

Other name spaces are used within subelements of the XML, like below:

Other Namespaces

The root node that we have to read is feed and then subsiquently move to the child nodes.Noe to start the correcponding class for this XML, add a new class file to your project as we will be needing many different classes to map to diff objects withing the same XML, its adviceable to add a namespace and then add corresponding classes to the same namespace for easier readability and association to one XML becomes easier.
I am calling the project "FlickPics" and adding a namespace "Classes" under FlickPics, here is a code snippet of the same, also added the XMLRoot Attribute, Remember to use System.Xml.Serialization namespace.

Adding Namespace

adding up of the root element maps the class to that particular element under XML (like here we are mapping "feed" to "FlickrFeed" class),it is very important to give the corresponding namespace of that element, else an error is thrown while deserializing the XML.

Read more: Impact work

Posted via email from jasper22's posterous

0 comments: