C# 阅读复杂的Rss提要

C# 阅读复杂的Rss提要,c#,xml,xpath,rss,xml-namespaces,C#,Xml,Xpath,Rss,Xml Namespaces,您好,我正在尝试阅读digg提要,但是我的代码没有从RSS返回任何项目,我认为名称空间是问题所在 <?xml version="1.0" encoding="utf-8"?> <feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:digg="http://digg.com/docs/diggrss/" xmlns:media="http://search.yahoo.com/mrss/"> <

您好,我正在尝试阅读digg提要,但是我的代码没有从RSS返回任何项目,我认为名称空间是问题所在

<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:digg="http://digg.com/docs/diggrss/" xmlns:media="http://search.yahoo.com/mrss/">
<title>Top News</title>
<subtitle>Top News</subtitle>
<updated>2012-03-10T13:27:08Z</updated>
<link href="http://services.digg.com/2.0/story.getTopNews?type=rss" rel="self"/>
<id>http://services.digg.com/2.0/story.getTopNews?type=rss</id>
<link href="http://pubsubhubbub.appspot.com/" rel="hub"/>
<author>
<name>Digg</name>
</author>
<entry>
<title>The Windows 8 Power Struggle: Metro vs Desktop and Why They Don't Mesh Well</title>
<link href="http://digg.com/news/technology/the_windows_8_power_struggle_metro_vs_desktop_and_why_they_don_t_mesh_well?utm_campaign=Feed%3A+http%3A%2F%2Fservices.digg.com%2F2.0%2Fstory.getTopNews%3Ftype%3Drss&amp;utm_medium=feed&amp;utm_source=diggapi"/>
<content type="html">Metro, Microsoft's new UI, is bold, a dramatic departure from anything the company has previously done in the desktop/laptop space, and absolutely great. It's tangible proof that Redmond really can design and build its own unique products and experiences. However, the transition from Metro's start, for desktop users, is jarring and worse yet, Desktop mode and Metro don't mesh well at all..</content>
<updated>2012-03-09T17:12:03Z</updated>
<digg:diggCount>
92
</digg:diggCount>
<digg:category>
Technology
</digg:category>
<digg:commentCount>
3
</digg:commentCount>
<media:thumbnail height="62" url="http://cdn1.diggstatic.com/story/the_windows_8_power_struggle_metro_vs_desktop_and_why_they_don_t_mesh_well/t.png" width="62"/>
<media:group>
<media:content height="160" url="http://cdn3.diggstatic.com/story/the_windows_8_power_struggle_metro_vs_desktop_and_why_they_don_t_mesh_well/l.png" width="160"/>
<media:content height="48" url="http://cdn1.diggstatic.com/story/the_windows_8_power_struggle_metro_vs_desktop_and_why_they_don_t_mesh_well/s.png" width="48"/>
<media:content height="120" url="http://cdn1.diggstatic.com/story/the_windows_8_power_struggle_metro_vs_desktop_and_why_they_don_t_mesh_well/m.png" width="120"/>
<media:content height="62" url="http://cdn1.diggstatic.com/story/the_windows_8_power_struggle_metro_vs_desktop_and_why_they_don_t_mesh_well/t.png" width="62"/>
</media:group>
<id>http://digg.com/news/technology/the_windows_8_power_struggle_metro_vs_desktop_and_why_they_don_t_mesh_well</id>
</entry>
我错了什么!?!?我遇到的另一个问题是如何在不同的名称空间中获取这个标记

<digg:category>
Technology
</digg:category>

技术

当您调用
mgr.AddNamespace(“ns”)时,Thakshttp://www.w3.org/2005/Atom”
,它定义了命名空间前缀
ns
,然后可以在XPath查询中使用该前缀但您必须实际使用它:

var xmlPath = "/ns:feed/ns:entry";

虽然如果我是你,我会在调用
mgr.AddNamespace(“ns”)时使用更具描述性的前缀,如
atom
,或
a
,http://www.w3.org/2005/Atom”
,它定义了命名空间前缀
ns
,然后可以在XPath查询中使用该前缀但您必须实际使用它:

var xmlPath = "/ns:feed/ns:entry";

虽然如果我是你,我会使用更具描述性的前缀,比如
atom
,或者
a

这里有一个使用Linq 2 xml的解决方案

XDocument xDoc = XDocument.Load(new StringReader(xml));
XNamespace atom = XNamespace.Get("http://www.w3.org/2005/Atom");
XNamespace digg = XNamespace.Get("http://digg.com/docs/diggrss/");
XNamespace media = XNamespace.Get("http://search.yahoo.com/mrss/");

var items = xDoc
            .Descendants(atom + "entry")
            .Select(x => new
            {
                Title = x.Element(atom + "title").Value,
                Link = x.Element(atom + "link").Attribute("href").Value,
                Category = x.Element(digg+"category").Value.Trim(),
                Thumbnail = x.Element(media+"thumbnail").Attribute("url").Value
            })
            .ToArray();

下面是一个使用LINQ2XML的解决方案

XDocument xDoc = XDocument.Load(new StringReader(xml));
XNamespace atom = XNamespace.Get("http://www.w3.org/2005/Atom");
XNamespace digg = XNamespace.Get("http://digg.com/docs/diggrss/");
XNamespace media = XNamespace.Get("http://search.yahoo.com/mrss/");

var items = xDoc
            .Descendants(atom + "entry")
            .Select(x => new
            {
                Title = x.Element(atom + "title").Value,
                Link = x.Element(atom + "link").Attribute("href").Value,
                Category = x.Element(digg+"category").Value.Trim(),
                Thumbnail = x.Element(media+"thumbnail").Attribute("url").Value
            })
            .ToArray();

You Rock,使用Linq 2 xml更容易操作You Rock,使用Linq 2 xml更容易操作doThaks我理解,所以想象一下如果我需要获得category标记,如果add mgr.AddNamespace(“digg“,”),我如何访问类别!?!?是的,没错。要访问标记,可以使用XPath,如
/ns:feed/ns:entry/digg:category
。Thaks我理解,所以想象一下如果我需要获得类别标记,如果add mgr.AddNamespace(“digg”),我如何访问类别!?!?是的,没错。要访问标记,可以使用XPath,比如
/ns:feed/ns:entry/digg:category