Building a Windows 8 RSS Reader: Listing 4

Creating the RSSClient class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Web.Syndication;
namespace VSMWinRTDemo.RSS
{
  public class RSSClient
  {
    public async Task<RSSFeed> GetFeeds(string url, int maxItems)
    {
      RSSFeed feeds = new RSSFeed();
      SyndicationClient client = new SyndicationClient();
      Uri feedUri = new Uri(url);
      try
     {
       SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);
       var topFeeds = feed.Items.OrderByDescending(x =>    
       x.PublishedDate).Take(maxItems).ToList();
       feeds.Title = feed.Title.Text;
       foreach (var item in topFeeds)
       {
         RSSItem feedItem = new RSSItem();
         feedItem.Title = item.Title.Text;
         feedItem.PublishedOn = item.PublishedDate.DateTime;
         var authors = from a in item.Authors
         select a.Name;
         feedItem.Author =
         String.Join(",", authors);
         feedItem.Content = item.Content !=
         null ? item.Content.Text : String.Empty;
         feedItem.Description = item.Summary !=
         null ? item.Summary.Text : String.Empty;
         var links = from l in item.Links
         select new RSSLink(l.Title, l.Uri);
         feedItem.Links = links.ToList();
         feeds.Items.Add(feedItem);
       }
      }
      catch (Exception ex)
      {
        throw ex;
      }
     return feeds;
    }
  }
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at vogelvision@gmail.com.