3

I'm looking for a C# "one-liner" (need not strictly be a single line, but very short is preferable) way to download an RSS feed from a given HTTP URL, and extract specific data. Robustness be damned. Something that doesn't require any external libraries.

Specifically I want to count the number of <item>s in the RSS. But some kind of LINQ method that could be reused to, say for example, return a list of the item <title> elements would be most useful, if it can be kept short.

1
  • Strongly disagree with closure. This is clearly a programming question. May 1, 2012 at 12:02

3 Answers 3

6
Regex.Matches(new WebClient().DownloadString("http://stackoverflow.com/feeds/question/7180063"), "<entry>").Count
2
  • +1 for staying true to "one-liner"
    – Nate
    Aug 24, 2011 at 18:03
  • @Nate Your answer lends itself to a cleaner one-liner too
    – Jimmy
    Aug 24, 2011 at 18:05
5

What about something like this:

var rssFeed = XDocument.Load("http://weblogs.asp.net/scottgu/rss.aspx");

var posts = from item in rssFeed.Descendants("item")
            select new
            {
                Title     = (string)item.Element("title"),
                Published = (DateTime?)item.Element("pubDate"),
                Url       = (string)item.Element("link"),
            };

Source.

0
3
SyndicationFeed.Load(XmlReader.Create("http://weblogs.asp.net/scottgu/rss.aspx")).Items.Count();
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.