1

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 Answers 2

3

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it was this...I missed the UserAgent... Thanks... Stan
0

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.

1 Comment

Remember to add the namespace to the XName delivered to .Descendants and .Element, or you might get 0 results. For Atom feeds this is static, so: private static string XName(string elementName) { return string.Concat("{http://www.w3.org/2005/Atom}", elementName); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.