Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a URL is a valid Feed

Tags:

c#

feed

argotic

I'm using Argotic Syndication Framework for processing feeds.

But the problem is, if I pass a URL to Argotic, which is not a valid feed (for example, http://stackoverflow.com which is a html page, not feed), the program hangs (I mean, Argotic stays in an infinity loop)

So, How to check if a URL is pointing to a valid feed?

like image 644
Mahdi Ghiasi Avatar asked Aug 16 '12 22:08

Mahdi Ghiasi


People also ask

How do I find RSS feed URL?

Find the RSS Feed URL Through the Page SourceRight click on the website's page, and choose Page Source. In the new window that appears, use the “find” feature (Ctrl + F on a PC or Command + F on a Mac), and type in RSS. You'll find the feed's URL between the quotes after href=.

Do all websites have RSS feeds?

Every major CMS offers an RSS feed by default, meaning an RSS exists for such sites whether the site's creators realize that or not. In these cases, you can use a simple URL hack to find the RSS feed. Around 25 percent of sites are built using WordPress, for example.

What is a feed URL?

An RSS feed is a formatted text document that contains all the important information about your show. It's hosted on a server and (usually) has a public URL/link so anyone can view or access its contents.

Why and where is RSS used?

Websites usually use RSS feeds to publish frequently updated information, such as blog entries, news headlines, episodes of audio and video series, or for distributing podcasts.


2 Answers

From .NET 3.5 you can do this below. It will throw an exception if it's not a valid feed.

using System.Diagnostics;
using System.ServiceModel.Syndication;
using System.Xml;

public bool TryParseFeed(string url)
{
    try
    {
        SyndicationFeed feed = SyndicationFeed.Load(XmlReader.Create(url));

        foreach (SyndicationItem item in feed.Items)
        {
            Debug.Print(item.Title.Text);
        }
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

Or you can try parsing the document by your own:

string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<event>This is a Test</event>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

Then try checking the root element. It should be the feed element and have "http://www.w3.org/2005/Atom" namespace:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">

References: http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx http://dotnet.dzone.com/articles/systemservicemodelsyndication

like image 119
Akira Yamamoto Avatar answered Oct 03 '22 04:10

Akira Yamamoto


you can use Feed Validation Service. It has SOAP API.

like image 21
Dmitry Khryukin Avatar answered Oct 03 '22 03:10

Dmitry Khryukin