/// <summary> /// self writing rss-parse /// only compatible with rss 2.0 /// instance : laozhao's blog /// </summary> /// <param name="rssSourceUri"></param> /// <returns></returns> private async Task<RssSource> ForgeRssSource(Uri rssSourceUri) { HttpClient client = new HttpClient(); Stream stream = await client.GetStreamAsync(rssSourceUri); XDocument doc = XDocument.Load(stream); var channel = doc.Root.Element("channel"); string rssTitle = ForgeHelper.GetElementValueOrDefault(channel, "title"); string rssLink = ForgeHelper.GetElementValueOrDefault(channel, "link"); string rssDescription = ForgeHelper.GetElementValueOrDefault(channel, "description"); string rssEditor = ForgeHelper.GetElementValueOrDefault(channel, "webMaster"); string rssUpdateString = ForgeHelper.GetElementValueOrDefault(channel, "lastBuildDate"); DateTime rssUpdate; if (string.IsNullOrEmpty(rssUpdateString) || DateTime.TryParse(rssUpdateString, out rssUpdate)) { rssUpdate = DateTime.MinValue; } RssSource source = new RssSource( rssSourceUri.ToString(), rssTitle, rssLink, rssDescription); var items = from rssItem in channel.Descendants("item") select new RssItem( ForgeHelper.GetElementValueOrDefault(rssItem, "link"), source, ForgeHelper.GetElementValueOrDefault(rssItem, "title"), ForgeHelper.GetElementValueOrDefault(rssItem, "pubDate"), String.Join(",", rssItem.Descendants("category").Select(x => x.Value)), ForgeHelper.GetElementValueOrDefault(rssItem, "description")); foreach (var item in items) { source.AddItem(item); } return source; }
public RssItem(string uniqueId, RssSource sourceGroup, string title = "", string subtitle = "", string description = "", string content = "", string imagePath = "Assets/LightGray.png") : base(uniqueId, title, subtitle, imagePath, description, content, sourceGroup) { }
/// <summary> /// using Windows.Web.Syndication /// </summary> /// <param name="feedUriString"></param> /// <returns></returns> private async Task<RssSource> ForgeRssSource(string feedUriString) { AddSourceProgressRing.IsActive = true; Windows.Web.Syndication.SyndicationClient client = new SyndicationClient(); Uri feedUri = new Uri(feedUriString); try { SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri); // This code is executed after RetrieveFeedAsync returns the SyndicationFeed. // Process the feed and copy the data you want into the FeedData and FeedItem classes. RssSource feedData = new RssSource(feedUri.ToString()); if (feed.Title != null && feed.Title.Text != null) { feedData.Title = feed.Title.Text; } if (feed.Subtitle != null && feed.Subtitle.Text != null) { feedData.Description = feed.Subtitle.Text; } if (feed.Items != null && feed.Items.Count > 0) { // Use the date of the latest post as the last updated date. feedData.LastBuildTime = feed.Items[0].PublishedDate.DateTime; feedData.CreateTime = feed.LastUpdatedTime.UtcDateTime; foreach (SyndicationItem item in feed.Items) { RssItem feedItem = null; // Handle the differences between RSS and Atom feeds. if (feed.SourceFormat == SyndicationFormat.Atom10) { if (item.Id != null) { feedItem = new RssItem(item.Id, feedData); feedItem.Link = new Uri(item.Id); } if (item.Content != null && item.Content.Text != null) { feedItem.Content = item.Content.Text; } } else if (feed.SourceFormat == SyndicationFormat.Rss20) { if (item.Links != null && item.Links.Count > 0) { feedItem = new RssItem(item.Links[0].Uri.ToString(), feedData); feedItem.Link = item.Links[0].Uri; } if (item.Summary != null && item.Summary.Text != null) { feedItem.Content = item.Summary.Text; } } if (item.Title != null && item.Title.Text != null) { feedItem.Title = item.Title.Text; } if (item.PublishedDate != null) { feedItem.CreateTime = item.PublishedDate.DateTime; } if (item.Authors != null && item.Authors.Count > 0) { feedItem.Author = item.Authors[0].Name.ToString(); } feedData.AddItem(feedItem); } } AddSourceProgressRing.IsActive = false; return feedData; } catch (Exception) { AddSourceProgressRing.IsActive = false; return null; } }