Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

RSS Feed Link Reader

, 5 Jun 2008
This article discusses the construction of a simple application that may be used to view RSS feeds from the desktop.
rssfeedspeedreader.zip
RSSFeedSpeedReader
RSSFeedSpeedReader.suo
RSSFeedSpeedReader
bin
Release
RSSFeedSpeedReader.exe
RSSFeedSpeedReader.pdb
Properties
Settings.settings
RSSFeedSpeedReader.csproj.user
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;


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;
        }


        /// <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;
                tvwRss.ExpandAll();

            }
            catch (Exception ex2)
            {
                // snitch
                MessageBox.Show(ex2.ToString(), "RSS Feed Initialization Failure");
            }
        }



        /// <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
            }
        }


        /// <summary>
        /// If the user selects a canned RSS feed from the drop down,
        /// load the text into the toolstrip RSS location text box 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());
        }



    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 5 Jun 2008
Article Copyright 2008 by salysle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid