Welcome to MSDN Blogs Sign in | Join | Help

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 –

 XML

<rss version="2.0">

  <channel>

    <item>

      <link>http://test.com</link>

      <title>Page Title</title>

    </item>

  </channel>

</rss>

JSON  -

{
   "Version":"2.0",
   "Channel": {
         "Items": [{
               "Link":"http://test.com",
               "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":"http://test.com",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. –

[XmlElement("description")]

public string Description

{

get

      {

            return _description;

      }

 

      set

      {

            _description = value;

      }

}

In this example (code provided below) I have created classes closely matching Rss specification (http://blogs.law.harvard.edu/tech/rss)

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

 

Published Thursday, April 12, 2007 8:47 PM by shahpiyush
Attachment(s): RssToJson.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Convert Rss to JSON - Serialize to JSON by using XmlSerialization and JavaScriptSerializer

Tuesday, May 01, 2007 3:09 AM by Nick Parsons

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

# re: Convert Rss to JSON - Serialize to JSON by using XmlSerialization and JavaScriptSerializer

Tuesday, September 04, 2007 1:51 PM by kaevans

Very cool!  Note that you can also do this out of the box using .NET 3.5.  

http://blogs.msdn.com/kaevans/archive/2007/09/04/use-linq-and-net-3-5-to-convert-rss-to-json.aspx

# re: Convert Rss to JSON - Serialize to JSON by using XmlSerialization and JavaScriptSerializer

Thursday, October 25, 2007 10:45 AM by Hank Lynch

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

Very nice.

# JavaScript Object Notation For Cross Domain Web Requests

Sunday, December 16, 2007 4:59 PM by Robert Robbins

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

# Blogs and RSS &raquo; Piyush Shah&#8217;s Blog : Convert Rss to JSON - Serialize to JSON by using &#8230;

# Piyush Shah s Blog Convert Rss to JSON Serialize to JSON by using | Green Tea Fat Burner

# Piyush Shah s Blog Convert Rss to JSON Serialize to JSON by using | Outdoor Decor

# Piyush Shah s Blog Convert Rss to JSON Serialize to JSON by using | debt settlement program

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker