1
votes

I have a problem for parsing a rss feed using c#. I used to use this method to load the feed. XDocument rssFeed = XDocument.Load(@url); But, when I notice when the feed has a xml-stylesheet this method crashes saying the xml is not well formated... Here's a rss feed that contains this tag http://www.channelnews.fr/accueil.feed?type=rss

What would be the best way to parse any rss feed using c#?

Thanks for your help

2

2 Answers

3
votes

This code works for me

    static XDocument DownloadPage()
    {
        var req = (HttpWebRequest)WebRequest.Create("http://www.channelnews.fr/accueil.feed?type=rss");
        req.UserAgent = "Mozilla";

        using(var response = req.GetResponse())
        using(var stream = response.GetResponseStream())
        using (var reader = new StreamReader(stream))
            return XDocument.Load(reader);
    }

Note, that if you omit setting UserAgent, then response will contain string 'DOS' that is defnintly not xml :)

0
votes

This one works nicer:

XDocument xdoc = XDocument.Load("http://pedroliska.wordpress.com/feed/");

var items = from i in xdoc.Descendants("item")
            select new
            {
                Title = i.Element("title").Value
            };

So now you can access the rss titles by doing a loop or something like:

items[0].Title

And just the code is pulling the title from the rss feed, you can pull the description, link, pubDate, etc.