Convert Rss to JSON – Serialize to JSON by using XmlSerialization and JavaScriptSerializer

JSON (Javascript Object Notation) – is a light weight data interchange format.  It essentially is composed of key-value pairs.  For Example –


<rss version=2.0>




      <title>Page Title</title>





   “Channel”: {
         “Items”: [{
               “Title”:”Page Title”

The beauty of JSON is it integrates easily with Javascript. Just use eval() function of JavaScript to parse. So to initialize the above JSON this should do the trick –

var rss = eval(‘({ “Version”:”2.0″, “Channel”: { “Items”: [{Link”:””,Title”:”Page Title” }]}}’);

Javascript does provide some level of Object orientation. So you could access the properties from the above object like –

var itemtitle = rss.Channel.Items[0].Title;
var rssVersion = rss.Version;

RSS (Really Simple Syndication) – is used to syndicate your content. Unless you were hibernating for the past 5 years, I’m sure you know what Rss is. 🙂

So let’s get to the meat of this article. How to Convert an Rss Feed to JSON?

First I will use XmlSerialization to Serialize/Deserialize Rss. The System.Xml.Serialization namespace is  used for this purpose. You will need to decorate all your properties with XmlSerialization attributes. E.g. –


public string Description




            return _description;





            _description = value;



In this example (code provided below) I have created classes closely matching Rss specification (

To Serialize you can use the Serialize() method of XmlSerializer class

private string Serialize()


string xml = string.Empty;


      using (StringWriter output = new StringWriter(new StringBuilder(), System.Globalization.CultureInfo.InvariantCulture))


            XmlSerializer xmlSerializer = new XmlSerializer(typeof(RssDocument));

            xmlSerializer.Serialize(output, this);

            xml = output.ToString();



      return xml;


To Deserialize you can use the Deserialize ) method of XmlSerializer class

private static T DeserializeFromXmlUsingStringReader<T>(string xml)


if (string.IsNullOrEmpty(xml))


            throw new ArgumentException(“xml”);



      XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

      using (StringReader strngReader = new StringReader(xml))


            return (T)xmlSerializer.Deserialize(strngReader);



This should create a Strongly typed representation of Rss.

To convert this Strongly typed Rss classes to Json format is actually very easy.  ASP.Net AJAX provides JavaScriptSerializer class in System.Web.Script.Serialization.JavascriptSerializer.

Of particular importance to this example in this table is Serialize(object) method.

public string ToJson()


System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

      return js.Serialize(this); //this is an instance of the strongly typed Rss Class


What’s cool is this technique can be used on any objects which uses XmlSerialization.

I have code example in the attachment below with example on how to Convert Rss to Json by pointing to a Url or loading from Xml string.

Note : To run the example you need to have ASP.Net 2.0 and Ajax Extensions. You can download it from here

Comments (9)

  1. Nick Parsons says:

    Nice post! Great idea of using XmlSerialization and JavascriptSerializer. I’ll try it out.

  2. Hank Lynch says:

    So I found this article after….after downloading JSONSharp and messing about with it for an afternoon.

    Very nice.

  3. In attempting to use the Microsoft AJAX Library 1.0 in a MSHelp2 collection (to be viewed in the Microsoft

  4. Julien says:

    Another solution is to rely on a 3rd party service like Superfeedr to handle this conversion (as well as normalization):