1

如何从此提要中获取“入口”节点

http://www.google.com/alerts/feeds/14392773026536511983/5526937985735563348

我尝试了 linq to xml,但我认为由于条目标签的现有属性,以下代码不起作用。

string url = "http://www.google.com/alerts/feeds/14392773026536511983/5526937985735563348";

WebClient c = new WebClient();

string xml = ASCIIEncoding.Default.GetString(c.DownloadData(url));

XDocument doc = XDocument.Parse(xml);

var entries = doc.Descendants("entry");

提前致谢,

4

2 回答 2

5

您没有指定命名空间。尝试这个:

XNamespace atom = "http://www.w3.org/2005/Atom";
var entries = doc.Descendants(atom + "entry");

顺便说一句,我不会为此使用 ASCII,或者DownloadData...用于WebClient.DownloadString为您处理编码。或者实际上,只需使用XDocument.Load(url)

例如:

string url = ...;

XDocument doc = XDocument.Load(url);
XNamespace atom = "http://www.w3.org/2005/Atom";
var entries = doc.Descendants(atom + "entry");
Console.WriteLine(entries.Count()); // Prints 20
于 2011-03-24T08:06:27.523 回答
1

此数据以 Atom 格式呈现,因此您需要根据标准规范对其进行解析。最简单的方法是查看开源代码。查看此链接,您需要“为 Atom、RDF 和 RSS 提要创建通用解析器”部分

于 2011-03-24T08:08:15.620 回答