/// <summary> /// Getting news from all feeds. Comparing them with the news, that already stored in DB and adding absent. /// </summary> public bool RefreshNews() { try { foreach (Feed feed in this.dataContext.Feeds) { try { XmlReader reader = XmlReader.Create(feed.Url); try { SyndicationFeed synFeed = SyndicationFeed.Load(reader); foreach (SyndicationItem item in synFeed.Items) { if (this.dataContext.News.Where(n => n.Idfeed == feed.Id).Where(n => n.Name.Equals(item.Title.Text)).Count() == 0) { News news = new News(); news.Name = item.Title.Text; news.Url = item.Links[0].Uri.ToString(); news.Idfeed = feed.Id; this.dataContext.News.Add(news); } } } catch { continue; } reader.Close(); } catch { continue; } } this.dataContext.SaveChanges(); this.FillNews(); return true; } catch { return false; } }
/// <summary> /// Getting news from specific feed and storing them in DB. If at least one news already present in DB - work of the method will be canceled. /// </summary> public bool GetNews(int FeedID) { try { this.FillNews(FeedID); if (this.News.Where(n=>n.Idfeed == FeedID).Count() > 0) { return true; } else { XmlReader reader = XmlReader.Create(this.dataContext.Feeds.Where(f => f.Id == FeedID).First().Url); SyndicationFeed synFeed = SyndicationFeed.Load(reader); foreach (SyndicationItem item in synFeed.Items) { News news = new News(); news.Name = item.Title.Text; news.Url = item.Links[0].Uri.ToString(); news.Idfeed = FeedID; this.dataContext.News.Add(news); } reader.Close(); this.dataContext.SaveChanges(); this.FillNews(FeedID); return true; } } catch { return false; } }