Click here to Skip to main content
6,630,901 members and growing! (19,327 online)
Email Password   helpLost your password?
Languages » C# » Applications     Intermediate License: The Code Project Open License (CPOL)

C#: Application to Browse and Read using RSS Feeds from your Desktop

By Prashant K Singh

This article describes the usage of the WebBrowser object in C# to read and maintain your favourite RSS Lists
C# (C# 1.0, C# 2.0, C# 3.0), .NET (.NET 2.0, .NET 3.0, .NET 3.5), Visual Studio (VS2005, VS2008), Dev
Posted:9 Jun 2008
Views:13,178
Bookmarked:26 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.16 Rating: 3.31 out of 5
2 votes, 22.2%
1
1 vote, 11.1%
2

3
3 votes, 33.3%
4
3 votes, 33.3%
5
ReadForSpeed-Source

Introduction

This article is aimed at explaining how the WebBrowser control in C# works and how to build an RSS Feed reader using several WebBrowser controls. It also explains how RSS feed XML files can be processed using functions provided by System.Xml namespace.

The application created here has the following features:

  1. RSS Feed subscription
  2. RSS Feed list maintenance
  3. RSS Feed Viewer
  4. Web Page Reader and Browser

Background

The application in this article has been developed using Visual Studio 2008, but in .NET Framework 2.0. It can be built and run as is using .NET Framework 3.0 and 3.5.

Using the Code

The code in this article works using WebBrowser controls which will be explained in detail later in the article. The code takes in an RSS Feed link and adds it to the Web content of the LHS pane. Any click on a feed in the RHS pane is directed towards loading the corresponding feed after being formatted using the XPathNavigator, in the RHS top pane. Any click on a feed link in this pane is redirected to loading the corresponding page in the RHS bottom pane. The user can further browse other pages in this pane by clicking on other links on this page. The user can move back and forth between browsed pages using the Forward and Back buttons in the toolbar.

Now, coming to the WebBrowser control. This control provides us with all the functionality needed to build a Web browsing application. I will go through all its methods and properties used in the application being built here. But first, let us see how we can fetch and process a page provided to us by an RSS Feed link.

Since RSS feeds are XML, we need to use XML objects to process it. For this, we need to import the namespace System.Xml.

using System.Xml;

Then we need to create an XML document and load up the RSS content into it:

XmlDocument RSSXml = new XmlDocument();
RSSXml.Load(txtURL.Text);

Then we need to get the list of nodes from this XML document, that can be used to display the feed content. After that, we go through each node and pull out display relevant information from it, like the title, link and description.

XmlNodeList RSSNodeList = RSSXml.SelectNodes("rss/channel/item");
StringBuilder sb = new StringBuilder();
foreach (XmlNode RSSNode in RSSNodeList)
{
    XmlNode RSSSubNode;
    RSSSubNode = RSSNode.SelectSingleNode("title");
    string title = RSSSubNode != null ? RSSSubNode.InnerText : "";
    RSSSubNode = RSSNode.SelectSingleNode("link");
    string link = RSSSubNode != null ? RSSSubNode.InnerText : "";
    RSSSubNode = RSSNode.SelectSingleNode("description");
    string desc = RSSSubNode != null ? RSSSubNode.InnerText : "";
    sb.Append("<font face='arial'><p><b><a href='");
    sb.Append(link);
    sb.Append("'>");
    sb.Append(title);
    sb.Append("</a></b><br/>");
    sb.Append(desc);
    sb.Append("</p></font>");
}

At the end of this, the string builder contains the content of RSS feed, as a well formatted HTML. This is where we start off with the WebBrowser control. The WebBrowser control can be loaded with a page using several ways. A URL can directly be loaded to the WebBrowser control using Navigate(Uri) function of the WebBrowser. However to load HTML content that has been created programmatically, we need to populate the DocumentText of the WebBrowser using:

RSSBrowser.DocumentText = sb.ToString();

Now we need to learn how to communicate between two browser controls. By default, if a link is clicked on a page loaded in a WebBrowser control, it loads the target page in the same control. In case we want it to load the content in another browser, we need to block this control and invoke the other control's navigation.

To do this, we need to understand the Navigating event of the WebBrowser control. This event is triggered before a WebBrowser control starts navigating to a new page. It can be used to stop the navigation by setting its event argument's Cancel value to true as in the following code:

private void RSSList_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    if (!m_bFromLoadEvent)
    {
        e.Cancel = true;
        NetBrowser.Navigate(e.Url);
    }
    else
    {
        m_bFromLoadEvent = false;
    }
}

This code, based on a boolean value, blocks the current object's navigation and navigates another WebBrowser control NetBrowser, to the target URL. This is how the different panes communicate with each other in the given application.

Lastly, we need to learn how to navigate back and forth between the pages already visited by the WebBrowser control. This is quite simple and can be done as follows:

if (NetBrowser.CanGoBack)
{
    NetBrowser.GoBack();
}

if (NetBrowser.CanGoForward)
{
    NetBrowser.GoForward();
}

The CanGoForward and CanGoBack properties show the history status in forward and backward directions. The GoBack() and GoForward() functions navigate back and forth in the history of visited pages.

Points of Interest

The application stores the list of subscribed RSS feeds in a text file in the same location as the program EXE. Coming soon in the next versions of ReadForSpeed are the features to tag feeds and store feed content for offline usage.

History

  • Version 1.0.0.0 updated on 06/10/2008

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Prashant K Singh


Member
I work as a Lead Game Programmer at FxLabs Studios, an India based 3D desktop game development firm.

I have been a software engineer for 4 years and have worked in technologies like ASP .Net, VB .Net, C#, C/C++ etc.

Out of all this, C++ is my favorite and I am continually amazed at the versatility of STL.

I am married, but no one knows about it. So don't tell anybody. Shhhhh....

I love my wife.

My hobbies include Coding and Analyzing People Behavior... Smile
Occupation: Team Leader
Company: Fx Labs Studios
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralNice.. PinmemberMark Pryce-Maher5:30 6 Apr '09  
General407 Proxy Authentication Required PinmemberMember 38744553:53 16 Jun '08  
GeneralRe: 407 Proxy Authentication Required PinmemberPrashant K Singh0:15 27 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jun 2008
Editor: Deeksha Shenoy
Copyright 2008 by Prashant K Singh
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project