Happy New Year everyone! I am writing this at the beginning of 2023. Yes, that’s right, 2023 and we are talking about RSS. RSS for those young enough to not know, stands for Really Simple Syndication. It was used a long time ago to allow a user to read a blog or news article from a software tool called a Feed Reader. These days I feel there will be a comeback as it is used pretty extensivly in Mastodon. Also, I’m feeling nostagic so I’m writing a modern RSS Feed Reader.
Enough Background How Do We Read RSS Feeds?
Back in the day this was a convoluted process of reading the XML into a reader then parsing the items and displaying the title, author, content, etc.
Today though it is really simple. In modern .NET you just add System.ServiceModel.Syndication
to your project using your favorite nuget method:
dotnet add package System.ServiceModel.Syndication
Then you just add a using statement:
using System.ServiceModel.Syndication;
Next you need to load the feed using an XMLReader and parse the data using the Syndication services:
var myFeedUrl = "https://spatacoli.com/index.xml";
using var xmlReader = XMLReader.Create(myFeedUrl);
var myFeed = SyndicationFeed.Load(xmlReader);
BAM! That’s it you now have an object myFeed
that contains the information from the feed. For example you can look at the author of the first item by:
var author = myFeed.Items.FirstOrDefault().Author.Text;
You can iterate through the Items with a foreach
and display all the items included in the feed. It’s really a lot simpler than what we used to have to do.
Now that’s all there is to it. Let me know if you’d like a more complex example.