RSS (Really Simple Syndication) is one of the newest technology to update live data. Almost all of the blogs these days support and publish Rss feeds. Reading Rss feeds is very simple you just need to parse the xml, read few tags and that's it. In this article we will see how we can make our own custom control which reads Rss feeds of different website. This control will also have the design features so when it reads any RSS from the supplied Url you can view it in the design view of the application.

Introduction 

RSS (Really Simple Syndication) is one of the newest technology to update live data. Almost all of the blogs these days support and publish Rss feeds. Reading Rss feeds is very simple you just need to parse the xml, read few tags and that's it. In this article we will see how we can make our own custom control which reads Rss feeds of different website. This control will also have the design features so when it reads any RSS from the supplied Url you can view it in the design view of the application.

Format of the Rss Feeds:

Let's first talk about the format of the Rss feeds so that we will have the basic understanding of the data. Each Rss feed contains many tags but the most important to us in this article are <link> and <title> tag.

<link> contains the url of the Rss feed data. This means that if I have 10 articles and I want to publish an Rss Feed of those 10 articles then my Rss feed will have 10 link tags which will contain the url of the article. Off course you must assume that we are providing the url of the article.

<title> tag contains the title of the Rss Feed. This means that it will have the titles for the 10 articles.

To better understand these tags let's see the Rss feed offered by www.codersource.net.

<title>Codersource.net</title>
<link>http://www.codersource.net/</link>

<description>
Win32, C++, Visual C++ resources - Codersource.net Provides articles on C++, Win32, C, MFC using Visual C++ compiler, articles on .Net, C# and programming topics with sufficient sample programs.
</description>
<language>en-us</language>
<lastBuildDate>Wed, 04 May 2005 08:35:14 GMT</lastBuildDate>
<copyright>Copyright Codersource.net</copyright>
<managingEditor>webmaster@codersource.net</managingEditor>
<webMaster>webmaster@codersource.net</webMaster>
<docs>http://backend.userland.com/rss</docs>
<ttl>60</ttl>

I have made the sections bold which we are going to use in this article. Those sections are as I already discussed <title> and <link>. Let me also show you that how does it look like when I display this Rss Feed with the control which we are going to make.

See how simple it has become to display the Rss Feeds with the use of the custom control. Now you can click on any title and it will take you to the details of that article.

Using the Code:

Let's now see the main code of this control. The complete code for the Rss Custom Control is attached in the zip file. You are free to use it.

The first thing we do is to declare important variables.

// This is the default feeds that it will fetch

private string link = "http://www.azamsharp.net";

// Making the label control

private Label feeds;

The label will display the Rss Feeds on the screen.

Here is the simple property that is used to set the url property.

// This property is used to set the Url to the Rss Feed

[Bindable(true),

Category("Appearance"),

DefaultValue("")]

public string Url
{

get
{

return link;

}

set
{

link = value; }

}

Now let's come to the main of the control which is the extracting of the Rss feeds.

// Fetches the Rss Feeds from the Url

private string GenerateRssFeeds(string url)
{

string title = null;

string link = null;

XmlTextReader reader = null;

StringBuilder sb = new StringBuilder();

sb.Append("<ul>");

try
{

reader = new XmlTextReader(url);

reader.MoveToContent();

while (reader.Read())
{

if (reader.Name == "item" && reader.NodeType == XmlNodeType.Element)
{

sb.Append("<li>");

}

if (reader.Name == "title")
{

title = reader.ReadString();

}

if (reader.Name == "link")
{

link = reader.ReadString();

}

if (reader.Name == "item" && reader.NodeType == XmlNodeType.EndElement)
{

sb.Append("<a href='");

sb.Append(link);

sb.Append("'>");

sb.Append(title);

sb.Append("</a>");

sb.Append("</li>");

}

}

return sb.ToString();

}
catch(Exception ex)
{

return "Your Feeds will appear here";

}

finally
{

reader.Close();

}

}

As you can see I am just using a while loop to go through the xml file. And we only extract the tags that we need. In this case those tags are <title> and <link>. Next we are using the StringBuilder object to format the links into hyperlinks. Please also keep in mind that you must be connected to Internet in order to read Rss feeds.

Finally we will look at the CreateChildControls method which is used to make the controls:

// Creates the Child Controls

protected override void CreateChildControls()
{

Controls.Clear();

feeds = new Label();

feeds.ID = "MyFeeds";

if(Url!=null && Url!="")

{

feeds.Text = GenerateRssFeeds(Url);

}

this.Controls.Add(feeds);

}

First we clear all the controls and make our label control. The text to the label is assigned using the GenerateRssFeeds method. We are also checking the if url is not null and not an empty string.

Let's see our control in action. Below is the screen shot of the msdn rss feeds.

So you see that using RssReader custom control have saved us a lot of coding. The whole project is attached as a Zip file and you are free to use it in any way.

I hope you liked the article, happy coding!