C# RSS Reader Publish Date Issue

2015/12/313 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Issue: SyndicationFeed Throws Exception
  3. Sample Data
  4. Sample Code
  5. Alternative Approach Using XDocument
  6. Conclusion

Introduction

This post covers an issue where C# SyndicationFeed throws an exception depending on the RSS feed item's Publish Date value, and provides an alternative approach using XDocument.

Issue: SyndicationFeed Throws Exception

Why?

Reading RSS feeds should be simple. W3C defines the format for RSS, and everyone should follow the same format and create libraries for it. You can use the W3C RSS validator tool to check your own RSS feed. Now, following is about how to read an RSS page with C#. C# provides SyndicationFeed for communicating with RSS. However, I've seen that sometimes C# code will throw an exception depending on the value of an RSS feed item's Publish Date. Following is an example that will throw an exception when reading the RSS.

Let's say you have the following type of RSS feed to read. You could get the RSS from this URL: http://www.asp.net/rss/content

Sample Data


Your First ASP.NET 5 Web App Using Visual Studio

http://docs.asp.net/en/latest/tutorials/your-first-aspnet-application.html


In this tutorial, you’ll create a simple web app using ASP.NET 5. The app stores data in a SQL database using Entity Framework (EF) and uses ASP.NET MVC to support the basic CRUD operations (create...

tutorials/your-first-aspnet-application
Wed, 16 Mar 2016 00:00:00

Sample Code

Now, this is the C# code that will read the above RSS and will throw an exception depending on the Publish Date value.

string url = "http://www.asp.net/rss/content";
 using (HttpClient client = new HttpClient())
            {
                var task = client.GetAsync(url);
                string result = task.Result.Content.ReadAsStringAsync().Result;

                using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(result)))
                {
                    using (XmlReader reader = XmlReader.Create(stream))
                    {
// Throw exception
                        SyndicationFeed feed = SyndicationFeed.Load(reader);

                    }
                }
}

Alternative Approach Using XDocument

This is another version that safely checks the RSS without using C# SyndicationFeed, but instead uses C# XDocument to read it.

Main Class to Read RSS

string url = "http://www.asp.net/rss/content";
            using (HttpClient client = new HttpClient())
            {
                var task = client.GetAsync(url);
                string result = task.Result.Content.ReadAsStringAsync().Result;

                using (StringReader sr = new StringReader(result))
                {
                    XDocument xd = XDocument.Load(sr);
                    foreach (var item in xd.Descendants("item"))
                    {
                        string link = XElementToString(item.Element("link"));
                        string title = XElementToString(item.Element("title"));
                        string desc = XElementToString(item.Element("description"));
                        string guid = XElementToString(item.Element("guid"));
                        DateTime pubDate = XElementToDate(item.Element("pubDate"));
// TODO: you can convert all other rss feed
                    }
                }
            }

Simple Helper Class to Safely Check the XElement

public static string XElementToString(XElement xElement)
        {
            if (xElement == null)
            {
                return string.Empty;
            }

            return xElement.Value;
        }

Conclusion

The code is straightforward. The key takeaway is that SyndicationFeed can fail on certain date formats, so using XDocument as an alternative gives you more control over RSS parsing. I might create a NuGet library for this in the future that reads all defined RSS feed fields in C# or perhaps in JavaScript too.