简体   繁体   中英

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.

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.

  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:

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 . You have to enumerate the children named content and each of those elements does have the attributes you're interested in.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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