Many websites and blogs have their content available via feeds that users and applications can subscribe to. Feeds basically are a simplified version of the site content, providing text and media –only representation of the data published on the website. One of the most popular feed formats is RSS and it stands for Really Simple Syndication. It is an XML-based format of the content and is used by the majority of websites that contain dynamically updated content, like blogs or news resources.
In this post, we will develop a simple console application to read RSS feeds with .NET Core.
Let's start by creating the console application with .NET Core:
- Open Visual Studio. In this post, we are using Visual Studio 2022 Community Version
- Set the project name as "ReadRSSFeeds"
- In the additional information, select .NET 6 for the framework
Install-Package System.ServiceModel.Syndication -Version 6.0.0
Once the package is installed, we need to make use of the using statement to add a reference to the Syndication namespace here is the syntax:
using System.ServiceModel.Syndication;
Now we need to right the main code to read and process the feeds, here is the C# syntax:
var url = "https://jamilhallal.blogspot.com/feeds/posts/default?alt=rss";
using var reader = XmlReader.Create(url);
var feed = SyndicationFeed.Load(reader);
Once the feed loaded, we can query each syndicated item as per the following code which will return the most recent published post in the blog:
var post = feed.Items.FirstOrDefault();
- Authors
- Categories
- Content
- Contributors
- Copyright
- Publish Date
- Links
Here is the final code in the class "Program.cs" of the .NET Core Console app:
// See https://aka.ms/new-console-template for more information
using System.Xml;
using System.ServiceModel.Syndication;
var url = "https://jamilhallal.blogspot.com/feeds/posts/default?alt=rss";
using var reader = XmlReader.Create(url);
var feed = SyndicationFeed.Load(reader);
var post = feed.Items.FirstOrDefault();
//Now, post represent the most recent published post in the blog.
Hi, I am getting a weird error when getting the rss from http://feeds.foxnews.com/foxnews/latest. It looks like it is coming with some bad chars but the SyndicationFeed.Load(reader) fails and it cannot download anything. Did you have this problem with some other RSS? So far I cannot find an answer.
ReplyDeleteThanks
Hey, no I didn't face similar issue. what is the error you are receiving?
Delete