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