Less code lines are almoast not possible! But one leak is available; the load method always download the full items and there is no option only to download item headers.
The formatted version is also available at my website: http://www.ronischuetz.com/code/reading_rss_atom_feeds.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.ServiceModel.Syndication;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
List<string> urls = new List<string>();
urls.Add("http://feedproxy.google.com/RoniSchuetz");
urls.Add("http://www.tagesanzeiger.ch/rss.html");
foreach (var item in urls)
{
Console.WriteLine(item);
Reader(item);
}
TimeSpan d = DateTime.Now - start;
Console.WriteLine(d.TotalMilliseconds);
Console.ReadLine();
}
private static int counter = 0;
private static void Reader(string url)
{
SyndicationFeed blogFeed = null;
try
{
// Read the feed using an XmlReader
using (XmlReader reader = XmlReader.Create(url))
{
// Load the feed into a SyndicationFeed
blogFeed = SyndicationFeed.Load(reader);
}
}
catch (Exception ex)
{
if (ex is WebException ex is XmlException)
{
// Handle bad url, timeout or xml error here.
Console.WriteLine("Handle bad url, timeout or xml error here: " + ex.Message);
}
else
Console.WriteLine("second case: " + ex.Message);
}
// Use the feed
foreach (SyndicationItem item in blogFeed.Items)
{
Console.WriteLine((++counter) + " " + item.Title.Text);
}
}
}
}
No comments:
Post a Comment