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>https://test.com</link>
<title>Page Title</title>
</item>
</channel>
</rss>
JSON -
{
"Version":"2.0",
"Channel": {
"Items": [{
"Link":"https://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":"https://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 (https://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
Comments
Anonymous
April 30, 2007
Nice post! Great idea of using XmlSerialization and JavascriptSerializer. I'll try it out.Anonymous
September 04, 2007
The comment has been removedAnonymous
October 25, 2007
So I found this article after....after downloading JSONSharp and messing about with it for an afternoon. Very nice.Anonymous
December 16, 2007
In attempting to use the Microsoft AJAX Library 1.0 in a MSHelp2 collection (to be viewed in the MicrosoftAnonymous
March 15, 2008
PingBack from http://blogrssblog.info/piyush-shahs-blog-convert-rss-to-json-serialize-to-json-by-using/Anonymous
June 09, 2009
PingBack from http://greenteafatburner.info/story.php?id=1772Anonymous
June 13, 2009
PingBack from http://outdoordecoration.info/story.php?id=1479Anonymous
June 15, 2009
PingBack from http://edebtsettlementprogram.info/story.php?id=22738Anonymous
November 30, 2015
Another solution is to rely on a 3rd party service like Superfeedr to handle this conversion (as well as normalization): blog.superfeedr.com/convert-rss-to-json