📜 ⬆️ ⬇️

The simplest feed reader in 10 minutes

I had an idea to write an article about how you can create your RSS reader. I even started to implement it, but yesterday Maxter’s article, Generating RSS feeds using the Syndication class, appeared at Habré. This article covered a set of Syndication classes for working with feeds (Feeds) RSS and Atom. Thanks to Syndication, you can not only generate feeds, but also read them. In this article I want to present the simplest example of an RSS / Atom reader that can be written in just 10 minutes. True, this feature is available only in .Net Framework 3.5 ...


So, let's begin



First, create a new project - a normal Windows application. With the simplest interface, like this:

')
The basis of the form is a TableLayoutPanel, with the parameter filling the form, and already there are TextBox, Button, ListView (for displaying a list of news) and WebBrowser (for displaying content). Next, add the “System.ServiceModel.Web” assembly to References, and add the following to the using section:
using System.ServiceModel.Syndication;

using System.Xml;
As for the code, it is the simplest. Here is the processing code for clicking on the "Update" button:
private void btRefresh_Click ( object sender, EventArgs e)
{
// Check if the address is set
if (! String .IsNullOrEmpty (tbUrl.Text))
{
// Clear the ListView before adding new data
lvNews.Clear ();

// Create an XmlReader to read RSS / Atom
XmlReader FeedReader = XmlReader.Create (tbUrl.Text);
// Download RSS / Atom
SyndicationFeed Channel = SyndicationFeed.Load (FeedReader);

// if loaded
if (Channel! = null )
{
// process every news feed
foreach (SyndicationItem RSI in Channel.Items)
{
// create an item to display in the ListView
ListViewItem LVI = new ListViewItem (RSI.Title.Text);
LVI.Name = RSI.Title.Text;
// link ListViewItem and news
LVI.Tag = RSI;
// add news to the listview
lvNews.Items.Add (LVI);
}
}
}
}

Now add the MouseDown event handler for the ListView:
private void lvNews_MouseDown ( object sender, MouseEventArgs e)
{
// if the left mouse button is pressed, then
if (e.Button == MouseButtons.Left)
{
// get the ListViewItem that is under the cursor
ListViewItem item = lvNews.GetItemAt (eX, eY);

// if the course is not from scratch, then
if (item! = null )
{
// get the news associated with the selected ListViewItem
SyndicationItem RSI = (SyndicationItem) item.Tag;

// print the full text of the news
if (RSI.Content! = null ) // means Atom
wbDescription.DocumentText = (((TextSyndicationContent) RSI.Content) .Text;
else // otherwise RSS
wbDescription.DocumentText = RSI.Summary.Text;
}
}
}

That's all. The simplest feed reader is ready. It can read both RSS and Atom feeds.

Yes, by the way, this example is written to demonstrate the usability of Syndication. Since I have not yet completed my article on how to write an Rss reader without it, here, for comparison, a link to another article on the same topic: Working with RSS in C # (Csharp). Create your own RSSReader .

UPD:
Link to download the source: RssReader.rar

Source: https://habr.com/ru/post/27389/


All Articles