【问题标题】:CNN media:content medium="image" SyndicationFeedCNN 媒体:content media="image" SyndicationFeed
【发布时间】:2018-05-13 13:03:55
【问题描述】:

我正在尝试从 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    
        }
    }
}

如何检索图像值?

【问题讨论】:

    标签: c# xml linq-to-xml syndication-feed


    【解决方案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;
    

    【讨论】:

    • 更简单:element.Attribute("url");
    【解决方案2】:

    尽量接近你所拥有的,这将在 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&lt;XElement&gt;() 确实为您提供了group 元素,但该元素本身没有mediumurl 属性。您必须枚举名为 content 的子元素,并且每个元素都具有您感兴趣的属性。

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

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

    【讨论】:

      【解决方案3】:

      这些项目没有任何值。这是 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>
      

      【讨论】:

        猜你喜欢
        • 2017-03-17
        • 2012-04-28
        • 2013-05-14
        • 1970-01-01
        • 2017-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-24
        相关资源
        最近更新 更多