1

我正在尝试从 CNN 的 RSS 提要中获取图像值,但在尝试获取它时遇到了问题。

我追求的是价值

 <media:content medium="image" 
                url="https://cdn.cnn.com/cnnnext/dam/assets/180513143656-police-masks-surabaya-hp-video.jpg" 
                height="144" 
                width="256" />  

使用下面的代码,我可以看到我在追求什么,但我无法检索该值。

foreach (SyndicationElementExtension extension in f.ElementExtensions)
{
    XElement element = extension.GetObject<XElement>();
    if (element.HasAttributes)
    {
        foreach (var attribute in element.Attributes())
        {
             //How to get value here    
        }
    }
}

如何检索图像值?

4

3 回答 3

1

您可以通过使用 Linq 来实现这一点。

  var urlElem = element.Attributes().FirstOrDefault(a => a.Name == "url");
  if(urlElem!=null)
  {
     Console.Write(urlElem.Value);
  }

不要忘记使用:

using System.Linq;
using System.Xml.Linq;
于 2018-05-13T14:43:20.633 回答
0

试图保持接近你所拥有的这将列出来自 CNN RSS 提要中的 medium="image" 的所有 url:

var ns = (XNamespace) "http://search.yahoo.com/mrss/";  // namespace of the extension

SyndicationFeed feed = SyndicationFeed.Load(XmlReader.Create(@"http://rss.cnn.com/rss/edition.rss"));

var urls = from item in feed.Items              // all items
           from ext in  item.ElementExtensions  // all extensions to ext
           where ext.OuterName == "group" &&    // find the ones called group
                 ext.OuterNamespace == ns       // in the right namespace
           from content in ext.GetObject<XElement>().Elements(ns + "content") // get content elements
           where (string) content.Attribute("medium") == "image"  // if that medium is an image
           select (string) content.Attribute("url");              // get the url

// output what is in urls
foreach(string url in urls) 
{
      Console.WriteLine(url);
}

最重要的是,它GetObject<XElement>()确实为您提供了group元素,但该元素本身没有属性mediumurl. 您必须枚举命名的子content元素,并且每个元素都具有您感兴趣的属性。

如果您还需要宽度和高度,您可以轻松地将它们投影到最终选择中:

select new { 
            url = (string) content.Attribute("url"),
            h = (int) content.Attribute("height"),
            w = (int) content.Attribute("width") 
           };
于 2018-05-13T15:36:47.177 回答
-1

这些项目没有任何值。这是 xml 的示例:

    <item>
      <title><![CDATA[Trump says he's working with China to save tech firm crushed by US ban]]></title>
      <link>http://money.cnn.com/2018/05/13/technology/business/trump-zte-corporation-china-commerce/index.html</link>
      <guid isPermaLink="true">http://money.cnn.com/2018/05/13/technology/business/trump-zte-corporation-china-commerce/index.html</guid>
      <pubDate>Sun, 13 May 2018 22:38:18 GMT</pubDate>
      <media:group>
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-super-169.jpg" height="619" width="1100" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-large-11.jpg" height="300" width="300" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-vertical-large-gallery.jpg" height="552" width="414" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-video-synd-2.jpg" height="480" width="640" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-live-video.jpg" height="324" width="576" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-t1-main.jpg" height="250" width="250" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-vertical-gallery.jpg" height="360" width="270" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-story-body.jpg" height="169" width="300" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-t1-main.jpg" height="250" width="250" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-assign.jpg" height="186" width="248" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510091942-zte-office-shanghai-hp-video.jpg" height="144" width="256" />
      </media:group>
    </item>
    <item>
      <title><![CDATA[US officials offer North Korea 'prosperity' for de-nuking]]></title>
      <link>https://www.cnn.com/collections/north-korea-intl-051318/</link>
      <guid isPermaLink="true">https://www.cnn.com/collections/north-korea-intl-051318/</guid>
      <pubDate>Sun, 13 May 2018 18:36:04 GMT</pubDate>
      <media:group>
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-super-169.jpg" height="619" width="1100" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-large-11.jpg" height="300" width="300" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-vertical-large-gallery.jpg" height="552" width="414" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-video-synd-2.jpg" height="480" width="640" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-live-video.jpg" height="324" width="576" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-t1-main.jpg" height="250" width="250" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-vertical-gallery.jpg" height="360" width="270" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-story-body.jpg" height="169" width="300" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-t1-main.jpg" height="250" width="250" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-assign.jpg" height="186" width="248" />
        <media:content medium="image" url="https://cdn.cnn.com/cnnnext/dam/assets/180510111120-20180510-trump-kim-illo-01-hp-video.jpg" height="144" width="256" />
      </media:group>
    </item>
于 2018-05-13T23:43:59.287 回答