C# RSS Reader Publish Date Issue

2015/12/313 min read
bookmark this
Responsive image

Issue: Use C# SyndicationFeed to read rss sometime throw exception

Why?

Reading rss feed should be simple, W3C define the format for RSS, everyone also should be just following the same format and create library for it. You can use the W3C RSS validator tool for check your own RSS feed or not. Now, following is about how to read rss page by C#. C# provide the SyndicationFeed for communicte with RSS. However I've seen sometime C# code will throw exception depend on what value of RSS Feed Item's Publish Date, following is a example will throw exception when reading the RSS.

Le'ts say you have following type of rss feed need to read. You could get the RSS from this url http://www.asp.net/rss/content

Sample Data

<item>
<title>Your First ASP.NET 5 Web App Using Visual Studio</title>
<link>
http://docs.asp.net/en/latest/tutorials/your-first-aspnet-application.html
</link>
<description>
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...
</description>
<guid>tutorials/your-first-aspnet-application</guid>
<pubDate>Wed, 16 Mar 2016 00:00:00</pubDate>
</item>

Sample Code

Now, this is the C# code will read the above RSS and will throw exception depend 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);

                    }
                }
}

This is my another version to safty check the RSS, which not use the C# SyndicationFeed but use 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
                    }
                }
            }

Just one simple helper class to safty check the XElement

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

            return xElement.Value;
        }

Conclusion

The code is very simple, I might create library as Nuget Library next time. The library read all the defined RSS feed in C# or maybe in javascript too. My question is... Does RSS will be dead?