C# read rss feed and display each item at a set time
Hi i am trying to make a C# forms application to read and show a RSS feed that update (e.g. every 5 mins) my current code to read the RSS feed as follows
public void ReadRSS()
{
string url = "https://www.theage.com.au/rss/feed.xml";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();
int RssItemCount = SyndicationFeed.Load(reader).Items.Count();
while (CurrentItem == RssItemCount)
{
foreach (SyndicationItem item in feed.Items)
{
String subject = item.Title.Text;
String summary = item.Summary.Text;
label1.Text = subject;
label2.Text = summary;
}
}
CurrentItem++;
}
i have a timer and the idea was at each tick to go to the next item in the feed, hence the CurrentItem variable. how do i display specific item from the feed?
thanks in advance
Solution 1:
Read the data into a list. Add a Timer to your form. Then, place your code to display the desired data inside the Timer.Tick event handler.
Try the following:
VS 2019:
- Create a Windows Forms App (.NET Framework)
Open Solution Explorer:
- In VS menu, select Solution Explorer
Add Load event handler to Form
- In Solution Explorer, right-click Form1.cs
- Select View Designer
- In Designer, double-click Form1 to add the Load event handler to the form
Add reference (System.ServiceModel)
- In VS menu, click Project
- Select Add Reference...
- Select Assemblies
- Check System.ServiceModel
Create class (name: FeedInfo.cs)
- In VS menu, click Project
- Select Add Class... (name: FeedInfo.cs)
FeedInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadRSS
{
public class FeedInfo
{
public string Subject { get; set; }
public string Summary { get; set; }
}
}
Modify Form1.cs code
- In Solution Explorer, right-click Form1.cs
- Select View Code
Form1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Diagnostics;
namespace ReadRSS
{
public partial class Form1 : Form
{
private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
private List<FeedInfo> feedInfos = new List<FeedInfo>();
private int currentIndex = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//ToDo: set to desired value
//timer1.Interval = 1000 * 60 * 5;
timer1.Interval = 1000; //ms
//subscribe to event (add event handler)
timer1.Tick += Timer1_Tick;
//get data
feedInfos = GetRSSInfo();
//Debug.WriteLine("feedInfos.Count: " + feedInfos.Count.ToString());
//start timer
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
//ToDo: add desired code
Debug.WriteLine("[{0}]: Subject: {1} Summary: {2}", currentIndex, feedInfos[currentIndex].Subject, feedInfos[currentIndex].Summary);
if (currentIndex < feedInfos.Count - 1)
currentIndex += 1; //increment
else
currentIndex = 0; //set value
}
private List<FeedInfo> GetRSSInfo()
{
List<FeedInfo> feedInfoList = new List<FeedInfo>();
string url = "https://www.theage.com.au/rss/feed.xml";
using (XmlReader reader = XmlReader.Create(url))
{
SyndicationFeed feed = SyndicationFeed.Load(reader);
int RssItemCount = feed.Items.Count();
foreach (SyndicationItem item in feed.Items)
{
string subject = item.Title.Text;
string summary = item.Summary.Text;
//Debug.WriteLine("Subject: " + subject + " Summary: " + summary);
//create new instance
FeedInfo fInfo = new FeedInfo() { Subject = subject, Summary = summary };
//add
feedInfoList.Add(fInfo);
}
}
return feedInfoList;
}
}
}
Additional Resources:
- SyndicationFeed.Load Method