2

我一直在寻找使用彭博 RSS 提要的解决方案。

请查看他们的 XML Bloomberg RSS 提要

如您所见,<itunes:summary>我想得到,但不知道如何获得。

目前,我正在使用这个

List<FeedModel> list = new List<FeedModel>();
XmlReader reader = XmlReader.Create(rssUri);

SyndicationFeed feed = SyndicationFeed.Load(reader);

var feedItems = from item in feed.Items
                select new FeedModel()
                {
                    Content = item.Summary.Text,
                    Id = item.Id,
                    PublishDate = item.PublishDate.DateTime.ToString(),
                    Title = item.Title.Text,
                    Link = item.Links.First().Uri.ToString()
                };
return feedItems.ToList();

我使用雅虎新闻,它工作正常,但不是彭博社。请帮忙?

额外信息

我是一名 php 开发人员,我需要用 c# 为我的公司开发一个小模块。

4

2 回答 2

2

这很简单:

var doc = XDocument.Load("http://www.bloomberg.com/feed/podcast/on-the-economy.xml");

XNamespace itunes = "http://www.itunes.com/dtds/podcast-1.0.dtd";


var query = doc.Root.Elements("channel")
                .Elements("item")
                .Select(p => new BloomergFeeds
                {
                    Title = p.Element("title").Value,
                    Link = p.Element("link").Value,
                    Guid = p.Element("guid").Value,
                    PublishDate = p.Element("pubDate").Value,
                    Itunes = new Itunes
                    {
                        Author = p.Elements(itunes + "author").First().Value,
                        Subtitle = p.Elements(itunes + "subtitle").First().Value,
                        Summary = p.Elements(itunes + "summary").First().Value,
                        Duration = p.Elements(itunes + "duration").First().Value,
                        Keywords = p.Elements(itunes + "keywords").First().Value,
                    }
                }).ToList();


 public class BloomergFeeds
 {
     public string Title { get; set; }
     public string Link { get; set; }
     public string Guid { get; set; }
     public string PublishDate { get; set; }
     public Itunes Itunes { get; set; }
  }

  public class Itunes
  {
     public string Author { get; set; }
     public string Subtitle { get; set; }
     public string Summary { get; set; }
     public string Duration { get; set; }
     public string Keywords { get; set; }
  }

输出格式很好:

在此处输入图像描述

显然,您可以更进一步,将关键字拆分为 的新属性List<string>,为链接添加新属性(附件)等。无论您想要什么。

享受。

于 2013-01-08T12:51:36.920 回答
1

这是一个如何获取扩展属性的快速示例。

用法

var model = GenerateModelFromRssFeed("http://www.bloomberg.com/feed/podcast/on-the-economy.xml");

代码

public class FeedModel
{
    public string Id { get; set; }
    public string Content { get; set; }
    public DateTime PublishDate { get; set; }
    public string Title { get; set; }
    public string Link { get; set; }
    public string Summary { get; set; }
}

public IEnumerable<FeedModel> GenerateModelFromRssFeed(string feedUri)
{
    if (string.IsNullOrWhiteSpace(feedUri)) throw new ArgumentNullException(feedUri);

    using (var reader = XmlReader.Create(feedUri))
    {

        SyndicationFeed feed = SyndicationFeed.Load(reader);
        if (feed == null) throw new ApplicationException("Bloomberg feed cannot be downloaded");

        return from item in feed.Items
                select new FeedModel
                {
                    // this looks a little ugly but is necessary to check if the object (Summary) is null as an 
                    // exception will be thrown when trying to access a property on a null object
                    Content = item.Summary == null ? string.Empty : item.Summary.Text,
                    Id = item.Id,
                    PublishDate = item.Summary == null ? DateTime.MaxValue : item.PublishDate.DateTime,
                    Title = item.Title == null ? string.Empty : item.Title.Text,
                    Link = item.Links.Any() ? item.Links.First().Uri.ToString() : string.Empty,

                    // add in additional logic to test if the element extension is not null
                    Summary = item.ElementExtensions.FirstOrDefault(x => x != null && x.GetObject<XElement>().Name.LocalName.ToLowerInvariant() == "summary").GetObject<XElement>().Value
                };
    }
}

您显然可以GenerateModelFromRssFeed根据需要将该方法设为静态。

于 2013-01-08T12:38:17.557 回答