开发者

RSS.NET unable to parse feedburner feeds

I'm using RSS.NET for .NET 2.0. Try as I might, I get 0 channels for the following:

feed = RssFeed.Read("http://feeds.feedburner.com/punchfire?format=xml");

I note开发者_如何学运维 that for other feeds this works e.g.

feed = RssFeed.Read("http://www.engadget.com/rss.xml");

I guess it has to be a valid xml document. Do you think I should check for ".xml" in my application code or is there any way to tweak RSS.NET into accepting feedburner feeds?


The reason you are not able to get any channel nodes is that, atom format doesn't have any channel nodes. check following

  • The Atom Syndication Format

Your second link is an Rss Feed, which contains channel node like as follows

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
<channel>
<title>Engadget</title>
<link>http://www.engadget.com</link>
 .
 .
 .

On the other hand an atom feed does not use channel nodes, as you might understand by following specification link above.

<?xml version="1.0" encoding="utf-8"?>
   <feed xmlns="http://www.w3.org/2005/Atom">
     <title type="text">dive into mark</title>
     <subtitle type="html">
       A &lt;em&gt;lot&lt;/em&gt; of effort
       went into making this effortless
     </subtitle>
     <updated>2005-07-31T12:29:29Z</updated>
     <id>tag:example.org,2003:3</id>
     <link rel="alternate" type="text/html"
      hreflang="en" href="http://example.org/"/>
     <link rel="self" type="application/atom+xml"
      href="http://example.org/feed.atom"/>
     <rights>Copyright (c) 2003, Mark Pilgrim</rights>
     <generator uri="http://www.example.com/" version="1.0">
       Example Toolkit
     </generator>
     <entry>
      .
      .
      .

EDIT: To check feed Format

    // url of the feed 
    string utlToload = @"http://feeds.feedburner.com/punchfire?format=xml"



    // load feed
        Argotic.Syndication.GenericSyndicationFeed feed = 
Argotic.Syndication.GenericSyndicationFeed.Create(new Uri(urlToLoad));

        // check what format is it
        if (feed.Format.Equals(SyndicationContentFormat.Rss))
        {
            // yourlogic here
        }
        else if (feed.Format.Equals(SyndicationContentFormat.Atom))
        {
            // yourlogic here
        } 
        else if (feed.Format.Equals(SyndicationContentFormat.NewsML))
        {
            // yourlogic here
        } 

Hope it helps


With .Net 3.5+ it's very easy to parse Atom feeds. BTW Atom feeds are the new standard for RSS I hear.

public class RssFeedDO
{
    public string RssFeedUrl { get; private set; }

    /// <summary>
    /// Build feed processor
    /// </summary>
    /// <param name="feedUrl">Atom or RSS url with http in front.</param>
    public RssFeedDO(string feedUrl)
    {
        this.RssFeedUrl = feedUrl;
    }

    /// <summary>
    /// Will try to get RSS data from url passed in constructor. Can do atom or rss
    /// </summary>
    /// <returns></returns>
    public SyndicationFeed GetRSSData()
    {
        SyndicationFeed feed = null;

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        settings.CheckCharacters = true;
        settings.CloseInput = true;
        settings.IgnoreComments = true;
        settings.IgnoreProcessingInstructions = true;
        settings.DtdProcessing = DtdProcessing.Prohibit;

        if (!string.IsNullOrEmpty(this.RssFeedUrl))
        {
            using (XmlReader reader = XmlReader.Create(this.RssFeedUrl, settings))
            {
                SyndicationFeedFormatter GenericFeedFormatter = null;
                Atom10FeedFormatter atom = new Atom10FeedFormatter();
                Rss20FeedFormatter rss = new Rss20FeedFormatter();

                if (reader.ReadState == ReadState.Initial)
                {
                    reader.MoveToContent();
                }
                // try either atom or rss reading
                if (atom.CanRead(reader))
                {
                    GenericFeedFormatter = atom;
                }
                if (rss.CanRead(reader))
                {
                    GenericFeedFormatter = rss;
                }
                if (GenericFeedFormatter == null)
                {
                    return null;
                }
                GenericFeedFormatter.ReadFrom(reader);
                feed = GenericFeedFormatter.Feed;
            }
        }
        return feed;
    }
}

Now you could point an object data source to the above class and then eval some things in your ListView or RadGrid like this:

<%# Eval("Title.Text") %>

Tested with a couple feedburner feeds and it worked fine.


I've improved JamesR's code a bit, which now returns a success boolean and accepts a ByRef syndicationfeed object;

public static class FeedParser
{
    public static bool Parse(string url, ref SyndicationFeed feed)
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        settings.CheckCharacters = true;
        settings.CloseInput = true;
        settings.IgnoreComments = true;
        settings.IgnoreProcessingInstructions = true;
        settings.DtdProcessing = DtdProcessing.Prohibit;

        if (string.IsNullOrEmpty(url)) return false;

        using (XmlReader reader = XmlReader.Create(url, settings))
        {
            SyndicationFeedFormatter formatter = null;
            Atom10FeedFormatter atom_formatter = new Atom10FeedFormatter();
            Rss20FeedFormatter rss_formatter = new Rss20FeedFormatter();

            if (reader.ReadState == ReadState.Initial) reader.MoveToContent();

            if (atom_formatter.CanRead(reader)) formatter = atom_formatter;
            if (rss_formatter.CanRead(reader)) formatter = rss_formatter;
            if (formatter == null) return false;

            formatter.ReadFrom(reader);
            feed = formatter.Feed;

            return true;
        }
    }        
}

which can be used like;

    SyndicationFeed feed=null;

    if(FeedParser.Parse(url,ref feed))
    {

    }


http://feeds.feedburner.com/punchfire?format=xml seems to be an Atom feed, not RSS. Does rss.net know how to handle that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜