|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article discusses the construction of a simple application that may be used to view RSS feeds from the desktop. The application allows the user to select a canned RSS feed or to key one. The RSS feed is opened and the article title and link nodes from the feed are placed into a
Figure 1: Application in Use.
The title of the article occupies the parent node with the link node placed as the child of each title node. If the user clicks on any of the link nodes, the article associated with the title will be opened into a Web browser control.
Figure 2: Canned RSS Feeds in drop down, current RSS feed on left side.
The intent of the application was to provide a simple tool that may be used to organize and read RSS postings. The titles and links are recovered from the XML returned from the RSS service. Getting StartedIn order to get started, unzip the included project and open the solution in the Visual Studio 2008 environment. In the solution explorer, you should note these files:
Figure 3: Solution Explorer.
As you can see, the project is a WinForms application containing only a single form. The form itself contains a The Main Form (frmRss.cs)The main form is used to open the selected RSS feed as an XML document and to display them in If you'd care to open the code view up in the IDE, you will see that the code file begins as follows: using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
Note that the additions of the Following the imports, the class is defined and a constructor added. A local namespace RSSFeedSpeedReader
{
public partial class frmRss : Form
{
// the current path to an RSS feed
string mRssUrl;
/// <summary>
/// Constructor
/// </summary>
public frmRss()
{
InitializeComponent();
// zeroize RSS feed line
mRssUrl = string.Empty;
}
Next up is the click event handler for the RSS Go button located on the form’s /// <summary>
/// Open up an RSS Feed into the treeview control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsRssGo_Click(object sender, EventArgs e)
{
try
{
// set the file path member var
mRssUrl = this.tsRssLocation.Text;
// Clear the treeview.
tvwRss.Nodes.Clear();
// set the wait cursor
this.Cursor = Cursors.WaitCursor;
// create a new XML doc
XmlDocument doc = new XmlDocument();
try
{
// load the XML doc
doc.Load(mRssUrl);
// return the cursor
this.Cursor = Cursors.Default;
}
catch (Exception ex1)
{
// return the cursor
this.Cursor = Cursors.Default;
// tell a story
MessageBox.Show(ex1.Message);
return;
}
// get an XPATH navigator
XPathNavigator navigator = doc.CreateNavigator();
try
{
// look for the path to the RSS item titles; navigate
// through the nodes to get all titles
XPathNodeIterator nodes =
navigator.Select("//rss/channel/item/title");
while (nodes.MoveNext())
{
// clean up the text for display
XPathNavigator node = nodes.Current;
string tmp = node.Value.Trim();
tmp = tmp.Replace("\n", "");
tmp = tmp.Replace("\r", "");
// add a new treeview node for this
// news item title
tvwRss.Nodes.Add(tmp);
}
// set a position counter
int position = 0;
// Get the links from the RSS feed
XPathNodeIterator nodesLink =
navigator.Select("//rss/channel/item/link");
while (nodesLink.MoveNext())
{
// clean up the link
XPathNavigator node = nodesLink.Current;
string tmp = node.Value.Trim();
tmp = tmp.Replace("\n", "");
tmp = tmp.Replace("\r", "");
// use the position counter
// to add a link child node
// to each news item title
tvwRss.Nodes[position].Nodes.Add(tmp);
// increment the position counter
position++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "RSS Feed Load Error");
}
// restore the cursor
this.Cursor = Cursors.Default;
}
catch (Exception ex2)
{
// snitch
MessageBox.Show(ex2.ToString(), "RSS Feed Initialization Failure");
}
}
The next section of code is used to load the link content into the Web browser control. This is accomplished using the /// <summary>
/// Use the after select event to open the link
/// into the Web browser control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvwRss_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
// get the first four characters from the link
string tmp = tvwRss.SelectedNode.Text.Substring(0, 4);
// test the link text and then
// navigate the browser to that link
if (tmp == "http")
webBrowser1.Navigate(tvwRss.SelectedNode.Text);
}
catch
{
// if it is not a link, skip it
}
}
The last method in the class is the handler for the canned feeds /// <summary>
/// If the user selects a canned RSS feed from the drop down,
/// load the text into the toolstrip RSS location textbox and
/// then use the tsRssGo_Click event to launch the
/// page into the browser
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsCboFeeds_SelectedIndexChanged(object sender, EventArgs e)
{
tsRssLocation.Text = tsCboFeeds.SelectedItem.ToString();
tsRssGo_Click(this, new EventArgs());
}
SummaryThis application demonstrates a simple way of building an RSS Feed reader through the manipulation of the RSS Feed’s XML. This is a little different than most of the RSS Feeder reader’s I have encountered in that it does not attempt to provide the standard set of information returned from the feed in a History
|
||||||||||||||||||||||||||||||||||||||||||||