Overview
In this post, we will see how to read RSS feeds from the C# Corner website and display the feeds in a Blazor project. We will see how to get all post feeds for an author (author ID can be given by the user), feeds for the latest posts, feeds for featured articles, and feeds for top-read posts.
RSS (originally RDF Site Summary; later, two approaches emerged, which used the acronyms “Rich Site Summary” and “Really Simple Syndication” respectively). It is a type of web feed that allows users and applications to access updates to the online content of a website in a standardized, computer-readable format.
Blazor is an experimental .NET web framework using C#/Razor and HTML that runs in the browser with WebAssembly. Blazor provides all the benefits of a client-side web UI framework using .NET on the client and optionally, on the server.
I have already written many articles on Blazor in C# Corner. If you are new to Blazor, please refer to the below articles to get started with Blazor.
- Single Page Application With Blazor And CosmosDB
- Blazor - CRUD Using PostgreSQL And Entity Framework Core
- Blazor - Connect With Amazon DynamoDB Using AWS SDK
- Blazor - Work With Cassandra API In Cosmos DB
- Localization In Blazor App Using Microsoft.JSInterop
- Blazor - Create SPA With Azure Database For MariaDB Server
- Blazor - Connect With Oracle Database In Amazon RDS
Create a Blazor project in Visual Studio
Create a new Blazor project in Visual Studio. I am using the free community edition of Visual Studio 2017.
Choose .NET Core -> ASP.NET Core Web Application template.

Currently, Blazor supports three different templates. We will choose the “Blazor (ASP.NET Core hosted)” template for our project.

Our solution will be ready in a few moments. Please note that there are three projects created in our solution - “Client”, “Server”, and “Shared”.

By default, Blazor created many files in these three projects. We can remove all the unwanted files like “Counter. cshtml”, “FetchData.cshtml”, “SurveyPrompt.cshtml” from the Client project and “SampleDataController.cs” file from the Server project and remove the “WeatherForecast.cs” file from the shared project too.
Create a new “Models” folder in the “Shared” project and add a “Feed” class file inside this folder. We will add two classes “AuthorPosts” and “Feed” in this file.
Feed. cs
using System;
using System.Collections.Generic;
namespace CsharpCornerRSSBlazor.Shared.Models
{
public class AuthorPosts
{
public string AuthorName { get; set; }
public IEnumerable<Feed> Feeds { get; set; }
}
public class Feed
{
public string Link { get; set; }
public string Title { get; set; }
public string FeedType { get; set; }
public string Author { get; set; }
public string Content { get; set; }
public DateTime PubDate { get; set; }
public string PublishDate { get; set; }
public Feed()
{
Link = "";
Title = "";
FeedType = "";
Author = "";
Content = "";
PubDate = DateTime.Today;
PublishDate = DateTime.Today.ToString("dd-MMM-yyyy");
}
}
}
In the “Feed” class, we have defined all the properties for RSS feeds such as “Link”, “Title”, “FeedType”, “Author”, “Content”, “PubDate” and “PublishDate”.
“PublishDate” is a string property that is also used for keeping post-published data but will be converted to a string format dd-MMM-yyyy. Mainly I added this additional property to avoid the confusion over different date formats used in various countries.
In the “AuthorPost” class, we keep “AuthorName” and an array of feeds for a given author.
Create an API controller in the “Server” project

We can add code for getting all posts for a given author.
Code for "All Posts" for a given author.
readonly CultureInfo culture = new CultureInfo("en-US");
[Route("allposts/{authorId}")]
[HttpGet]
public AuthorPosts AllPosts(string authorId)
{
AuthorPosts authorPosts = new AuthorPosts();
try
{
XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/members/" + authorId + "/rss");
var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i =>
i.Name.LocalName == "item")
select new Feed
{
Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
FeedType = (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("blog") ? "Blog" : (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("news") ? "News" : "Article",
Author = item.Elements().First(i => i.Name.LocalName == "author").Value
};
authorPosts.AuthorName = entries.FirstOrDefault()?.Author;
authorPosts.Feeds = entries.OrderByDescending(o => o.PubDate);
return authorPosts;
}
catch
{
authorPosts.AuthorName = "NOT FOUND!";
List<Feed> feeds = new List<Feed>();
Feed feed = new Feed();
feeds.Add(feed);
authorPosts.Feeds = feeds;
return authorPosts;
}
}
We have passed the author ID as a parameter. Currently, C# Corner returns “links” for “Blogs” without a full URL. So, I added a ternary operator to add the full URL for blog posts. I have also checked the “link” value to differentiate post types as “Articles”, “Blogs” and “News”
Code for Latest Posts
[Route("latestposts")]
[HttpGet]
public IEnumerable<Feed> LatestPosts()
{
try
{
XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/rss/latestcontentall.aspx");
var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
select new Feed
{
Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
FeedType = (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("blog") ? "Blog" : (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("news") ? "News" : "Article",
Author = item.Elements().First(i => i.Name.LocalName == "author").Value
};
return entries.OrderByDescending(o => o.PubDate);
}
catch
{
List<Feed> feeds = new List<Feed>();
Feed feed = new Feed();
feeds.Add(feed);
return feeds;
}
}
The only difference in this method from the previous method is, that we gave the RSS feed URL statically.
Code for Featured Posts
[Route("featuredposts")]
[HttpGet]
public IEnumerable<Feed> FeaturedPosts()
{
try
{
XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/rss/featuredarticles.aspx");
var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
select new Feed
{
Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,
PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),
PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),
Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
FeedType = (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("blog") ? "Blog" : (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("news") ? "News" : "Article",
Author = item.Elements().First(i => i.Name.LocalName == "author").Value
};
return entries.OrderByDescending(o => o.PubDate);
}
catch
{
List<Feed> feeds = new List<Feed>();
Feed feed = new Feed();
feeds.Add(feed);
return feeds;
}
}
This is the same as the previous method, the only difference is the RSS feed URL.
Code for Top Read Posts









Pastrulo RamoneliPosted Oct 16, 2019, 3:10 PM
Hey Brother, I know is a year later, but could yo do this on newest Blazor? as it has changed dramatically, I get this error now: https://i.redd.it/3dnu0m4rtwr31.png
Satyaprakash SamantarayPosted Nov 20, 2018, 6:07 AM
This one is very helpful article and thanks for sharing the knowledge on blazor.
Mahesh ChandPosted Nov 20, 2018, 5:40 AM
This is great, Sarath. You've done a great job on Blazor. Best
Laxmidhar SahooPosted Nov 19, 2018, 11:03 PM
I read the whole article and feel a fantastic naration thanks a lot
Rushi MehtaPosted Nov 19, 2018, 9:40 PM
NIce Article