tags:

views:

81

answers:

2

I'm currently using the code below to attempt to check for a certain root node (rss) and a certain namespace\prefix (itunes), but it seems to be saying that the feed is valid even when supplied with a random web page URL instead of one pointing to a feed.

FeedState state = FeedState.Invalid;

XmlDocument xDoc = new XmlDocument();
xDoc.Load(_url);

XmlNode root = xDoc.FirstChild;
if (root.Name.ToLower() == "rss" && root.GetNamespaceOfPrefix("itunes") == "http://www.itunes.com/dtds/podcast-1.0.dtd")
{
    state = FeedState.Valid;
}

return state;

Can anybody tell me why this might be?

A: 

So you're saying you have stepped into your code and it gets to state = FeedState.Valid;? No matter what rss feed you try? Really? :)

aquinas
I hadn't had time to put in a breakpoint and have a look at the time of posting, but the outcome seems to be that way, yes.
A: 

Found the solution now. Putting xDoc.Load(_url); in a try .. catch block and returning FeedState.Invalid upon exception seems to have solved my problems.

FeedState state = FeedState.Invalid;

XmlDocument xDoc = new XmlDocument();

try
{
    xDoc.Load(_url);
}
catch
{
    return state;
}

XmlNode root = xDoc.FirstChild;
if (root.Name.ToLower() == "rss" && root.GetNamespaceOfPrefix("itunes") == "http://www.itunes.com/dtds/podcast-1.0.dtd")
{
    state = FeedState.Valid;
}

return state;