0

我目前正在使用下面的代码来尝试检查某个根节点(rss)和某个命名空间\前缀(itunes),但似乎是说即使提供了随机网页 URL,提要也是有效的而不是指向一个提要。

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;

谁能告诉我为什么会这样?

4

1 回答 1

0

现在找到了解决方案。把 xDoc.Load(_url); 在 try .. catch 块中并在异常时返回 FeedState.Invalid 似乎解决了我的问题。

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;
于 2009-08-11T11:08:37.193 回答