2012-03-10 14 views
0

こんにちはI'amはDiggのフィードを読み取ろうが、私のコードのdoesntのは、RSSから任意の項目を返さないを読んで、私は名前空間は、コードコンプレックス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> 

だと思います

var xmlPath = "/feed/entry"; 


    var xmlDoc = new XmlDocument(); 
    var webClient = new WebClient(); 
    var stream = new MemoryStream(webClient.DownloadData("http://services.digg.com/2.0/story.getTopNews?type=rss")); 
    xmlDoc.Load(stream); 


    var mgr = new XmlNamespaceManager(xmlDoc.NameTable); 
    mgr.AddNamespace("ns", "http://www.w3.org/2005/Atom"); 


    XmlNodeList xmlNode = xmlDoc.SelectNodes(xmlPath, mgr); 

    int count = xmlNode.Count; 

    Console.WriteLine("Count : "+count); 

    //Console.WriteLine(xmlNode.Name + ": " + xmlNode.InnerText); 
    Console.ReadKey(); 

私はdoginが間違っています!?!?その他Problema私は

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

Thaks

答えて

2

異なる名前空間にこのタグを取得する方法あなたはmgr.AddNamespace("ns", "http://www.w3.org/2005/Atom")を呼び出すされてきたときに、それはあなたが、あなたのXPathクエリで使用できる名前空間接頭辞nsを、定義されています。 しかし、あなたが実際にそれを使用する必要があります。

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

を私があなただったら、私は​​、またはaのようなより説明接頭辞を、使用することになりますが。 //ディグ:

+0

Thaks私は理解し、私はmgr.AddNamespaceを追加する場合( "ディグ" をカテゴリタグを取得する必要がある場合には想像を使用したソリューション、「httpです。 com/docs/diggrss/")、どのように私はこのカテゴリにアクセスしていますか? – mastervv

+0

はい、まさにです。タグにアクセスするには、 '/ ns:feed/ns:entry/digg:category'のようにXPathを使用できます。 – svick

2

は、ここでの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(); 
+0

あなたはロック、Linq 2 xmlでより簡単に行う – mastervv