这是xml:
 <item>
      <title>El Reno Tornado 5.31.13</title>
      <pubDate>Sat, 01 Jun 2013 14:01:12 -0400</pubDate>
      <link>http://url.com</link>
      <dc:creator>plankbone</dc:creator>
      <description>Chase video next to this monster tornado in El Reno Oklahoma. Check out the people driving on the wrong side of the road to escape it's path!</description>
      <guid>id</guid>
      <enclosure type="application/x-shockwave-flash" url="http://www.url.com" />
      <media:content>
        <media:player url="http://www.url.com" />
        <media:credit role="author" scheme="http://www.ur.com">plankbone</media:credit>
        <media:thumbnail url="http://url.com/80281E/u/u/thumbs/2013/Jun/1/5a1ee391e2dd_thumb_2.jpg" width="120" height="90" />
        <media:title>El Reno Tornado 5.31.13</media:title>
        <media:category label="Tags">el reno tornado, tornadoes, twisters, storm chasing,</media:category>
      </media:content>
    </item>
这是我的代码,除了从 xml 文件中获取缩略图数据外,一切正常。
                XNamespace dcM = "http://search.yahoo.com/mrss/";
                var xdoc = XDocument.Load(url);
                var items = xdoc.Descendants("item")
                .Select(item => new
                {
                    Title = item.Element("title").Value,
                    Description = item.Element("description").Value,
                    Link = item.Element("link").Value,
                    PubDate = item.Element("pubDate").Value,
                    MyImage = (string)item.Elements(dcM + "content")
                   .Select(i=> i.Elements(dcM + "thumbnail"))
                   .Select(i => i.Attribute("url").Value)
                   .SingleOrDefault()
                })
                .ToList();
由于某些原因,我无法从 xml 中获取值。
任何形式的帮助表示赞赏。
这是一个在元素(内容)中没有另一个元素(缩略图)时有效的示例:
可以说xml代码是:
<media:thumbnail url="http://ur.com/bla.jpg" />
这会起作用:
MyImage = (string)item.Elements(dcM + "thumbnail")
                   .Select(i => i.Attribute("url").Value)
                   .SingleOrDefault()