日期:2014-05-20 浏览次数:20896 次
public class NewsItem { public DateTime PostDate { get; set; } public string Title { get; set; } public string SourceURL { get; set; } public string Content { get; set; } }
public class NewsModels { public static async Task<List<NewsItem>> GetNews(string RssURI) { var x = XElement.Load(RssURI); DateTime _out = default(DateTime); return x.Element("channel").Elements("item") .Select(y => new NewsItem() { Content = (y.Element("description") ?? new XElement("description")).Value, PostDate = DateTime.TryParse((y.Element("pubDate") ?? new XElement("pubDate", default (DateTime))).Value, out _out) ? DateTime.Parse((y.Element("pubDate") ?? new XElement("pubDate", default (DateTime))).Value) : default(DateTime), SourceURL = (y.Element("link") ?? new XElement("link")).Value, Title = (y.Element("title") ?? new XElement("title")).Value }).ToList(); } }
@model IEnumerable<MvcApplication1.Models.NewsItem> @{ ViewBag.Title = "News"; } 用时 @ViewBag.TimeCost @foreach (var item in Model) { <h2>@item.Title</h2> <p>@item.PostDate</p> <p><a href="@item.SourceURL" target="_blank">@item.SourceURL</a></p> <p>@MvcHtmlString.Create(item.Content)</p> <p /> }
public class HomeController : AsyncController
public async Task<ActionResult> NewsAsyncAsync() { DateTime dt1 = DateTime.Now; var rss = new string[] { "http://articles.csdn.net/api/rss.php?tid=1008", "http://aspnet.codeplex.com/project/feeds/rss", "http://solidot.org.feedsportal.com/c/33236/f/556826/index.rss", "http://www.codeguru.com/icom_includes/feeds/codeguru/rss-csharp.xml", "http://feed.google.org.cn/" }; List<List<NewsItem>> list = new List<List<NewsItem>>(); foreach (var item in rss) { List<NewsItem> news = await (NewsModels.GetNews(item)); list.Add(news); } var model = list.SelectMany(x => x) .ToList() .OrderByDescending(x => x.PostDate) .Take(100); DateTime dt2 = DateTime.Now; ViewBag.TimeCost = new TimeSpan(dt2.Ticks - dt1.Ticks).ToString(); return View("News", model); }