How to Serialize/Deserialize Complex XML in ASP.Net / C#
// June 30th, 2011 // ASP.NET, C#
Have you ever wanted 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,
The namespace used here is:
- xmlns=”http://www.w3.org/2005/Atom”
Other name spaces are used within subelements of the XML, like below:
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 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.
Next, lets examine the elements under the feed now,
In the image above you can see that Feed has various elements underneath, we can translate what all we need in our class, like i did mapping of Title element with Variable string here:
one interesting thing to note down is that the root feed contains multiple entry elements, most of the information that we need to read is under this element, to translate this element to correspoint element in our class, create a new class with the name you want and declare an array of that under the “FlickrFeed” class we created, its shown by the code snippet below:
Entries Class
In the Entries Class i only translated what was required, you can always map the other elements by adding corresponding variables in your class.Now as there are 2 link elements under each entry element we have to take link as an array of class Link, below snippet shows the Link class created for mapping of different link elements,also note that each link element has different attributes to map.
Multiple Link Elements
Link Class
Author Class and Author XML Element Below is code snippet for the Author Element and Author Class
Once the class is created now comes the Deserialization Part, this is very simple and self explanatory(complete code is also available from the link at the bottom of this article).
Now the whole XML is deserialized to the FlickrFeed class and can be used as you want,just traverse through the FlickrFeed object and retrieve all the values needed.
Click here to download the source for this article , If you have any query related to the above article please click here to email.
-
http://blog.impact-works.com/2011/07/02/using-yahoo-placefinder-api-to-fetch-longitude-latitude-asp-net-csharp/ Using Yahoo placefinder API to fetch Longitude/Latitude : : Impact Works
-
http://blog.sina.com.cn/u/2207564265 calories blog
-
http://www.impact-works.com Parvesh Malhotra
-
http://www.buyfanpagefans.com/ free facebook fans software
- said: can you send me your code snippets on parvesh at i...
- tusto said: I already set publish_action and publish_stream an...
- said: please email me on parvesh at impact-works.com...
- Thusitha said: Thanks, No problem, I can send our details. Would ...
- said: yes you may use this, do send me your website link...
- said: can you send me screenshot or some other info on e...
- Posting to Facebook Wall in ASP.NET/C# using Graph API–Part 5
- Importing User info using facebook Graph API - Part 1
- Reading Facebook user's Wall with ASP.NET / C# using Facebook Graph API - Part 3
- Updating / Editing Facebook Event via Graph API using ASP.NET / C# – Part 7
- Creating Event(s) on Facebook with ASP.NET / C# using Graph API–Part 4



