简体   繁体   English

CNN媒体:内容媒体=“图片”联合供稿

[英]CNN media:content medium=“image” SyndicationFeed

I'm trying to get the image value out of CNN's RSS feed, but I'm having problems trying to get it. 我正在尝试从CNN的RSS提要中获取图像值,但是在尝试获取它时遇到了问题。

What I after is the value from 我追求的是从中获得的价值

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

With the code below I can see what I'm after, but I just cannot retrieve the value. 通过下面的代码,我可以看到我要执行的操作,但是我无法检索该值。

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    
        }
    }
}

How do I retrieve the image value? 如何获取图像值?

You can achive this by using Linq. 您可以使用Linq实现此目的。

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

Dont forget the using: 不要忘记使用:

using System.Linq;
using System.Xml.Linq;

Trying to stay close to what you have this will list all url's from the medium="image" in an CNN rss feed: 尝试与您保持联系,将在CNN rss feed中列出来自medium =“ image”的所有网址:

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);
}

Most significant is that GetObject<XElement>() did give you the group element but that element by it self doesn't have the attributes medium and url . 最重要的是, GetObject<XElement>()确实为您提供了group元素,但是该元素本身不具有mediumurl属性。 You have to enumerate the children named content and each of those elements does have the attributes you're interested in. 您必须枚举名为content并且每个元素确实具有您感兴趣的属性。

If you need the width and height as well, you can easily project them in the final select: 如果还需要宽度和高度,则可以在最终选择中轻松投影它们:

select new { 
            url = (string) content.Attribute("url"),
            h = (int) content.Attribute("height"),
            w = (int) content.Attribute("width") 
           };

The items do not have any values. 这些项目没有任何值。 Here is a sample of the xml : 这是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM