2010-10-16 78 views
1

我试图解析rss提要。我可以得到的元素,但无论我尝试,我无法获得属性!使用XDocument和LINQ读取属性

这是RSS提要(XML)的样子:

<?xml version="1.0" encoding="UTF-8" ?> 
<rss version="2.0"> 
    <channel> 
    <title>nu.nl - Algemeen</title> 
    <copyright>Copyright (c) 2010, nu.nl</copyright> 
    <link>http://www.nu.nl/algemeen/</link> 
    <language>nl</language> 
    <description>nu.nl Rich Site Summary</description> 
    <pubDate>Sat, 16 Oct 2010 08:36:37 +0200</pubDate> 
       <item> 
     <title>Jason W. keert zich af van moslimextremisme</title> 
     <link>http://www.nu.nl/binnenland/2357342/jason-w-keert-zich-af-van-moslimextremisme.html</link> 
     <guid>http://www.nu.nl/binnenland/2357342/index.html</guid> 
     <description>AMSTERDAM - Het vermeende lid van de Hofstadgroep Jason W. heeft het moslimextremisme naar eigen zeggen de rug toegekeerd.</description> 
       <related> 
      <b>Gerelateerd nieuws:</b><br /> 
            - <a href="http://www.nu.nl/binnenland/2293903/nieuw-proces-leden-hofstadgroep.html">Nieuw proces tegen leden Hofstadgroep</a><br /> 
            - <a href="http://www.nu.nl/binnenland/2175558/proces-hofstadgroep-moet.html">Proces Hofstadgroep moet over</a><br /> 
            - <a href="http://www.nu.nl/algemeen/2073580/eu-hof-verwerpt-beroep-van-lid-hofstadgroep.html">EU-hof verwerpt beroep van lid Hofstadgroep</a><br /> 
        </related> 
       <pubDate>Sat, 16 Oct 2010 08:36:36 +0200</pubDate> 
     <category>Algemeen</category> 
       <enclosure url="http://media.nu.nl/m/m1cz4nqakofe_t.jpg" type="image/jpeg" />  </item> 

我想要得到的属性是 '圈地URL'。

这是我的尝试:

private IEnumerable<Channel> getChannelQuery(XDocument xdoc) 
    { 

     return from channels in xdoc.Descendants("channel") 
      select new Channel 
      { 
       Title = channels.Element("title") != null ? channels.Element("title").Value : "", 
       Link = channels.Element("link") != null ? channels.Element("link").Value : "", 
       Description = channels.Element("description") != null ? channels.Element("description").Value : "", 
       //PubDate = channels.Element("pubDate") != null ? channels.Element("pubDate").Value : "", 
       //Enclosure = channels.Element("enclosure ") != null ? channels.Element("enclosure").Value : "", 
       Items = from items in channels.Descendants("item") 
        select new Item 
        { 
         Title = items.Element("title") != null ? items.Element("title").Value : "", 
         Link = items.Element("link") != null ? items.Element("link").Value : "", 
         Description = items.Element("description") != null ? items.Element("description").Value : "", 
         Guid = (items.Element("guid") != null ? items.Element("guid").Value : ""), 
         PubDate = (items.Element("pubDate") != null ? items.Element("pubDate").Value : ""), 
         Enclosure = (items.Attribute("url") != null ? items.Attribute("url").Value : "") 
        } 
      }; 
    } 

什么我错在这里做什么?

+0

你是怎么做到的? – Yustme 2010-10-16 11:26:29

回答

2

是不是

Enclosure = (items.Element("enclosure").Attribute("url") != null ? items.Element("enclosure").Attribute("url").Value : "") 

+0

是的,工作!但是如何?外壳不是一个元素吗?它不应该是一个属性? – Yustme 2010-10-16 14:49:04

+0

@Yustme:我觉得你很困惑,因为这个机箱是一个自动关闭的标签,它仍然是一个元素,因为它开始于< – 2010-10-16 15:12:39

+0

啊好吧,这是我用xml玩的一段时间。感谢您为我清理这个! – Yustme 2010-10-16 18:18:58