从Bloomberg新闻处获取RSS Feed

时间:2013-01-08 12:00:49

标签: c# asp.net rss

我一直在寻找消耗bloomberg rss feed的解决方案。

请查看他们的XML Bloomberg rss feed

正如你所看到的,我想得到<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();

我消费yahoo news它运作良好但不是彭博。请帮忙吗?

额外信息

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

2 个答案:

答案 0 :(得分: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; }
  }

输出格式很好:

enter image description here

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

享受。

答案 1 :(得分: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方法设为静态。