【发布时间】:2015-08-05 10:59:00
【问题描述】:
我正在使用 Html Agility 包从 rss xml 中选择文本数据。对于其他所有节点类型(标题、发布日期、guid 等),我可以使用 XPath 约定选择内部文本,但是在查询“//link”或实际上“item/link”时会返回空字符串。
public static IEnumerable<string> ExtractAllLinks(string rssSource)
{
//Create a new document.
var document = new HtmlDocument();
//Populate the document with an rss file.
document.LoadHtml(rssSource);
//Select out all of the required nodes.
var itemNodes = document.DocumentNode.SelectNodes("item/link");
//If zero nodes were found, return an empty list, otherwise return the content of those nodes.
return itemNodes == null ? new List<string>() : itemNodes.Select(itemNode => itemNode.InnerText).ToList();
}
有人知道为什么这个元素的行为与其他元素不同吗?
附加:运行“item/link”返回零节点。运行“//link”会返回正确数量的节点,但内部文本的长度为零。
使用下面的测试数据,使用“//name”为“fred”返回一条记录,但是使用“//link”返回一条带有空字符串的记录。
<site><link>Hello World</link><name>Fred</name></site>
我确定它是因为世界“链接”。如果我将其更改为“linkz”,它会完美运行。
以下解决方法非常有效。但是我想了解为什么在“//link”上搜索不像其他元素那样工作。
public static IEnumerable<string> ExtractAllLinks(string rssSource)
{
rssSource = rssSource.Replace("<link>", "<link-renamed>");
rssSource = rssSource.Replace("</link>", "</link-renamed>");
//Create a new document.
var document = new HtmlDocument();
//Populate the document with an rss file.
document.LoadHtml(rssSource);
//Select out all of the required nodes.
var itemNodes = document.DocumentNode.SelectNodes("//link-renamed");
//If zero nodes were found, return an empty list, otherwise return the content of those nodes.
return itemNodes == null ? new List<string>() : itemNodes.Select(itemNode => itemNode.InnerText).ToList();
}
【问题讨论】:
标签: c# xpath rss html-agility-pack