views:

408

answers:

3

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?

A: 

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

Anders Lindahl
yes it does. i'm trying to see how to route to the appropriate code based on the url.
Farooq
actually no, i'm trying to use Atom.NET for that purpose...
Farooq
+1  A: 

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

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"&gt;
<channel>
<title>Engadget</title>
<link>http://www.engadget.com&lt;/link&gt;
 .
 .
 .

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"&gt;
     <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/"/&gt;
     <link rel="self" type="application/atom+xml"
      href="http://example.org/feed.atom"/&gt;
     <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

Asad Butt
it figures. thanks for the help. i can use the atom parser for RSS.NET. do you know any way to determine just by looking at the url whether a feed is rss or atom?
Farooq
it is the most tricky part and unfortunately, until you have loaded the file from the source, it is not possible to check what format is it in. (I am not sure, but we used this)
Asad Butt
A: 

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.

James R