我正在使用Argotic联合框架来处理提要。
但问题是,如果我向Argotic传递一个不是有效提要的URL (例如,http://stackoverflow.com是一个html页面,而不是提要),程序就会挂起(我的意思是,Argotic停留在一个无限循环中)。
那么,如何检查URL是否指向有效的提要呢?
发布于 2012-08-17 06:46:28
在.NET 3.5中,你可以在下面这样做。如果它不是一个有效的提要,它将抛出一个异常。
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;
}
}或者,您可以尝试使用自己的方式解析文档:
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<event>This is a Test</event>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);然后尝试检查根元素。它应该是feed元素,并具有"http://www.w3.org/2005/Atom“名称空间:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">参考资料:http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx http://dotnet.dzone.com/articles/systemservicemodelsyndication
发布于 2012-08-17 07:01:50
您可以使用Feed Validation Service。它有SOAP API。
发布于 2012-08-17 06:49:04
您可以检查内容类型。它必须是text/xml。请参阅this question以查找内容类型。
您可以使用以下代码:
var request = HttpWebRequest.Create("http://www.google.com") as HttpWebRequest;
if (request != null)
{
var response = request.GetResponse() as HttpWebResponse;
string contentType = "";
if (response != null)
contentType = response.ContentType;
}感谢的回答
更新
要检查它是否是提要地址,您可以使用W3C Feed Validation服务。
Update2
正如BurundukXP所说,它有一个SOAP API。要使用它,您可以阅读this question的答案。
https://stackoverflow.com/questions/11996430
复制相似问题