LINQ TO XML 解析 RSS 源

发布于 2024-11-05 06:57:15 字数 1243 浏览 2 评论 0原文

我正在尝试使用 LINQ to Xml 解析 RSS 提要

这是 RSS 提要: http://www.surfersvillage.com/rss/rss.xml

我的代码如下为了测试

List<RSS> results = null;

XNamespace ns = "http://purl.org/rss/1.0/";
XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

XDocument xdoc = XDocument.Load("http://www.surfersvillage.com/rss/rss.xml");

results = (from feed in xdoc.Descendants(rdf + "item")
           orderby int.Parse(feed.Element("guid").Value) descending
           let desc = feed.Element("description").Value
           select new RSS
           {
               Title = feed.Element("title").Value,
               Description = desc,
               Link = feed.Element("link").Value
           }).Take(10).ToList();

代码,我在 Linq 查询的第一行放置了一个断点,并在中间窗口中使用以下内容对其进行了测试:

xdoc.Element(ns + "channel");

这可以正常工作并返回一个对象,正如

我输入的那样:

xdoc.Element(ns + "item");

上面的工作并返回一个对象,但是我正在寻找所有项目,

所以我输入了。

xdoc.Elements(ns + "item");

即使有超过 10 个项目,这也不会返回任何内容,后代方法也不起作用,并且还返回 null。

谁能给我一些指示,指出我哪里出错了?我也尝试用前面的 rdf 替换名称空间。

谢谢

I'm trying to parse an RSS feed using LINQ to Xml

This is the rss feed:
http://www.surfersvillage.com/rss/rss.xml

My code is as follows to try and parse

List<RSS> results = null;

XNamespace ns = "http://purl.org/rss/1.0/";
XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

XDocument xdoc = XDocument.Load("http://www.surfersvillage.com/rss/rss.xml");

results = (from feed in xdoc.Descendants(rdf + "item")
           orderby int.Parse(feed.Element("guid").Value) descending
           let desc = feed.Element("description").Value
           select new RSS
           {
               Title = feed.Element("title").Value,
               Description = desc,
               Link = feed.Element("link").Value
           }).Take(10).ToList();

To test the code I've put a breakpoint in on the first line of the Linq query and tested it in the intermediate window with the following:

xdoc.Element(ns + "channel");

This works and returns an object as expect

i type in:

xdoc.Element(ns + "item");

the above worked and returned a single object but I'm looking for all the items

so i typed in..

xdoc.Elements(ns + "item");

This return nothing even though there are over 10 items, the decendants method doesnt work either and also returned null.

Could anyone give me a few pointers to where I'm going wrong? I've tried substituting the rdf in front as well for the namespace.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏大泽ㄣ 2024-11-12 06:57:15

您引用了错误的名称空间。所有元素都使用默认名称空间而不是 rdf,因此您的代码应如下所示:

List<RSS> results = null;

XNamespace ns = "http://purl.org/rss/1.0/";
XDocument xdoc = XDocument.Load("http://www.surfersvillage.com/rss/rss.xml");
results = (from feed in xdoc.Descendants(ns + "item")
           orderby int.Parse(feed.Element(ns + "guid").Value) descending
           let desc = feed.Element(ns + "description").Value
           select new RSS
           {
               Title = feed.Element(ns + "title").Value,
               Description = desc,
               Link = feed.Element(ns + "link").Value
           }).Take(10).ToList();

You are referencing the wrong namespace. All the elements are using the default namespace rather than the rdf, so you code should be as follow:

List<RSS> results = null;

XNamespace ns = "http://purl.org/rss/1.0/";
XDocument xdoc = XDocument.Load("http://www.surfersvillage.com/rss/rss.xml");
results = (from feed in xdoc.Descendants(ns + "item")
           orderby int.Parse(feed.Element(ns + "guid").Value) descending
           let desc = feed.Element(ns + "description").Value
           select new RSS
           {
               Title = feed.Element(ns + "title").Value,
               Description = desc,
               Link = feed.Element(ns + "link").Value
           }).Take(10).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文