LINQ TO XML 解析 RSS 源
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您引用了错误的名称空间。所有元素都使用默认名称空间而不是 rdf,因此您的代码应如下所示:
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: