c# - 如何从 RSS 提要项中获取所有可能的图像 URL?

标签 c# .net image url syndication-feed

我尝试使用此示例从 http://www.nydailynews.com/cmlink/NYDN.Article.rss 获取图像 url

但没有成功

你能帮我找到所有正确的方法来通过 SyndicationItem 类从 RSS 提要项目中获取所有可能的图像 URL 吗?

有解决方案草案here但我想应该是更通用的解决方案。

谢谢!

 List<RssFeedItem> rssItems = new List<RssFeedItem>();
                    Stream stream = e.Result;
                    XmlReader response = XmlReader.Create(stream);
                    SyndicationFeed feeds = SyndicationFeed.Load(response);
                    foreach (SyndicationItem f in feeds.Items)
                    {
                        RssFeedItem rssItem = new RssFeedItem();

                        rssItem.Description = f.Summary.Text;
foreach (SyndicationLink enclosure in f.Links.Where<SyndicationLink>(x => x.RelationshipType == "enclosure"))
                            {
                                Uri url = enclosure.Uri;
                                long length = enclosure.Length;
                                string mediaType = enclosure.MediaType;
                                rssItem.ImageLinks.Add(url.AbsolutePath);
                            }
}

最佳答案

我找到了解决方案。

foreach (SyndicationElementExtension extension in f.ElementExtensions)
{
    XElement element = extension.GetObject<XElement>();

    if (element.HasAttributes)
    {
        foreach (var attribute in element.Attributes())
        {
            string value = attribute.Value.ToLower();
            if (value.StartsWith("http://") && (value.EndsWith(".jpg") || value.EndsWith(".png") || value.EndsWith(".gif") ))
            {
                   rssItem.ImageLinks.Add(value); // Add here the image link to some array
             }
        }                                
    }                            
}

关于c# - 如何从 RSS 提要项中获取所有可能的图像 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10539428/

相关文章:

iphone - 如何通过 HTTP post 将数据字节发送到 .NET 服务器

java - 通过 BufferedImage 从文件路径加载图像

c# - 什么可能导致性能提高? GC 时间、池化

c# - 如何使用我自己的方法或为 EF Core 查询编写 DbFunction (EF Core 3.0)

c# - 提高 WCF 服务性能

android - 从图库中的选定图像中获取图像的确切文件大小

python - 在 python 枕头中堆叠多个图像

c# - 如何将自定义类传递到 RDLC 报告并使用它?

c# - 将进程作为 Windows 服务运行时的最佳做法是什么?

c# - 对如何在 C# 中使用 JSON 感到困惑