使用LINQ读取RSS,比传统的C#2.0方法节省了很多代码
来源:互联网 发布:手机上可以开淘宝网店 编辑:程序博客网 时间:2024/04/30 16:49
private List<RssNews> GetNews() { List<RssNews> list = new List<RssNews>(); //http://services.community.microsoft.com/feeds/feed/CSharpHeadlines string rss = ConfigurationManager.AppSettings["NewsFeed"]; if (!string.IsNullOrWhiteSpace(rss)) { try { var feed = XDocument.Load(rss, LoadOptions.PreserveWhitespace); var items = from item in feed.Root.Element("channel").Elements("item").Take(5) select new RssNews() { title = item.Element("title").Value, link = item.Element("link").Value.ToSEOLink(), description = item.Element("description").Value, pubDate = item.Element("pubDate").Value }; list = items.ToList(); } catch { } } return list; }