СМИ CNN: среда контента = "изображение" SyndicationFeed - PullRequest
0 голосов
/ 13 мая 2018

Я пытаюсь получить значение изображения из RSS-канала CNN, но у меня возникают проблемы при попытке его получить.

Что мне нужно, это значение из

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

Как получить значение изображения?

Ответы [ 3 ]

0 голосов
/ 13 мая 2018

Попытка остаться рядом с тем, что у вас есть, перечислит все URL-адреса из среды = "image" в канале CNN rss:

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>() дал вам *Элемент 1005 *, но этот элемент сам по себе не имеет атрибутов medium и url.Вы должны перечислить дочерние элементы с именем content, и у каждого из этих элементов есть интересующие вас атрибуты.

Если вам нужны ширина и высота, вы можете легко спроецировать их в окончательном выборе.:

select new { 
            url = (string) content.Attribute("url"),
            h = (int) content.Attribute("height"),
            w = (int) content.Attribute("width") 
           };
0 голосов
/ 14 мая 2018

У предметов нет значений.Вот пример 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>
0 голосов
/ 13 мая 2018

Вы можете достичь этого, используя Linq.

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

Не забудьте использовать:

using System.Linq;
using System.Xml.Linq;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...