public string Export(int articleId) { Article article = null; using (Container = new ArticleContext()) { article = Container.Articles.FirstOrNothing(c => c.ArticleId == articleId).Bind(Mapper.Map <Entities.Article, Article>).GetOrDefault(null); TransactionHelper.AddTransaction(Container, ActionType.Export, null, _currentUser); Container.SaveChanges(); } var xmlAricle = new XmlArticle { Article = article }; using (var stream = XmlArticle.Serialize(xmlAricle.GetType(), xmlAricle)) { stream.Position = 0; var document = new XDocument(new XElement("Articles")); var anotherDocument = XDocument.Load(stream); document.Root.Add(anotherDocument.Root.Elements().First()); return(document.ToString()); } }
public override string Export() { using (Container = new ArticleContext()) { TransactionHelper.AddTransaction(Container, ActionType.Export, null, _currentUser); Container.SaveChanges(); } var all = GetAll(new QueryParams <Article>(0, 999, c => c.ArticleId)); var allXml = all.Select(c => new XmlArticle { Article = c }).OrderBy(c => c.Article.ArticleId).ToList(); using (var stream = XmlArticle.Serialize(allXml.GetType(), allXml)) { stream.Position = 0; var document = new XDocument(new XElement("Articles")); var anotherDocument = XDocument.Load(stream); foreach (var xNode in anotherDocument.Root.Elements().Select(c => c.Elements().First())) { document.Root.Add(xNode); } return(document.ToString()); } }
public int Save(Article entity, bool now = true) { var xmlArticle = new XmlArticle { Article = entity }; var duplicated = Document.Root.Elements().FirstOrDefault(c => c.Attribute("Link").Value == entity.Link); if (duplicated != null) { if (now) { throw new Exception("Найдена статья с таким же источником. Статья не сохранена."); } else { _builder.Append("Найдена повторяющиеся статья для \"{0}\". Статья не сохранена.".Fmt(duplicated.Attribute("ArticleName").Value)); } } xmlArticle.Article.GroupId = xmlArticle.Article.ArticleGroup.GroupId; xmlArticle.Article.CreationDate = DateTime.Now; using (var stream = xmlArticle.Serialize()) { stream.Position = 0; var currentDocument = XDocument.Load(stream); if (currentDocument.Root != null) { var element = currentDocument.Root.Elements().First(); if (duplicated != null) { return(-1); } else { AppendElement(element, "ArticleId", "ImageId"); if (now) { Document.Save(Path); } var id = int.Parse(element.Attribute("ArticleId").Value); entity.ArticleId = id; return(id); } } return(-1); } }
public Article Read(int id) { if (Document.Root.Elements().Any()) { var res = Document.Root.Elements().FirstOrDefault(c => c.Attribute("ArticleId").Value == id.ToString()); if (res != null) { var attr = res.Attribute("CreationDate"); if (string.IsNullOrEmpty(attr.Value)) { attr.Value = default(DateTime).ToString(); } var textReader = new StringReader(res.ToString()); return(XmlArticle.Deserialize(textReader)); } } return(null); }
private List <string> MergeAndImport(XDocument anotherDocument) { var unMatchedlist = new List <string>(); var matchedList = new List <Article>(); var currentList = GetAll(new QueryParams <Article>(0, 999, c => c.ArticleName)).ToList(); using (Container = new ArticleContext()) { var anotherList = new List <Article>(); //var anotherList = anotherDocument.Root.Elements().Select(xElement => new StringReader(xElement.ToString())).Select(XmlArticle.Deserialize).ToList(); XElement element = new XElement("Article"); try { foreach (var xElement in anotherDocument.Root.Elements()) { element = xElement; var stringReader = new StringReader(xElement.ToString()); var article = XmlArticle.Deserialize(stringReader); anotherList.Add(article); } } catch (Exception ex) { throw new Exception(string.Format("Статья {0} - не читается", element.Attribute("ArticleName")), ex); } foreach (Article article in anotherList) { Article match = currentList.FirstOrDefault(c => c.ArticleName == article.ArticleName || c.Link == article.Link); if (match == null) { matchedList.Add(article); } else { unMatchedlist.Add(string.Format("Статья '{0}' обнаружена в базе и не будет сохранена", match.ArticleName)); } } SaveMany(matchedList); return(unMatchedlist); } }
static void ReadArticles() { string xmlContent; DateTime now = DateTime.Now; DateTime prevMinute = now.AddMinutes(-60); MongoRepo <CatalogueSource> repoSource = new MongoRepo <CatalogueSource>(MongoDbCollections.CatalogueSource); Tuple <HttpWebResponse, List <CatalogueSource> > result = repoSource.GetAll(); if (result.Item1.StatusCode == HttpStatusCode.OK) { using (var wc = new WebClient()) { wc.Encoding = Encoding.UTF8; var xmlDoc = new XmlDocument(); int index = 0; foreach (var source in result.Item2) { Console.WriteLine(string.Format("{0} -> {1} fetching...", index, source.Name)); if (index > 20) { break; } try { xmlContent = wc.DownloadString(string.Format(urlPattern, source.Id)); xmlDoc.LoadXml(xmlContent); XmlArticle response = null; XmlSerializer serializer = new XmlSerializer(typeof(XmlArticle)); using (XmlReader reader = new XmlNodeReader(xmlDoc)) { try { response = (XmlArticle)serializer.Deserialize(reader); List <Model.Article> newArticles = new List <Model.Article>(); Model.Article newArticle = null; foreach (var item in response.Articles.Article) { if (!item.ArticleId.HasValue) { continue; } DateTime pubDate = Convert.ToDateTime(item.ArticlePubDate); try { newArticle = new Model.Article(); newArticle.Active = true; newArticle.Approved = false; newArticle.Body = item.ArticleBody; newArticle.DetailedDate = item.ArticleDetailedDate; newArticle.Id = item.ArticleId.Value; newArticle.ImageUrl = item.ArticleImageUrl; newArticle.InsertedAt = now; newArticle.PubDate = pubDate; newArticle.SharingTitle = item.ArticleSharingTitle; newArticle.SourceId = source.Id; newArticle.SourceUrl = item.SourceImageUrl; newArticle.ArticleUrl = item.ArticleUrl; newArticle.Title = item.ArticleTitle; newArticle.TweetId = item.TwitterId; newArticle.VideoUrl = item.ArticleVideoUrl; newArticle.Approved = false; newArticle.CategoryId = 1; newArticle.FavoriteHits = 0; newArticle.Hits = 0; newArticle.LikeHits = 0; newArticle.UnlikeHits = 0; newArticles.Add(newArticle); } catch (Exception ex) { logger.addLog(item.ArticleId + logger.s + "Not Converted Local Format" + logger.s + ex.ToString()); } } try { MongoRepo <Model.Article> repoArticle = new MongoRepo <Model.Article>(MongoDbCollections.Article); Tuple <HttpWebResponse, string> resultArticle = repoArticle.Put(newArticles); logger.addLog("SUCCESSFULLY" + logger.s + resultArticle.Item2 + logger.s + source.Name, true); Console.WriteLine(string.Format("\t{0} completed. Items: {1} Result: {2}", source.Name, newArticles.Count, resultArticle.Item2)); } catch (Exception ex) { logger.addLog(source.Name + logger.s + "Not Saved to Database" + logger.s + ex.ToString()); } } catch (Exception ex) { logger.addLog(source.Name + logger.s + "Not Deserialize" + logger.s + ex.ToString()); } } } catch (Exception ex) { logger.addLog(source.Name + logger.s + "Not Downloaded" + logger.s + ex.ToString()); } index++; } } } logger.addLog("Excuted comleted"); logger.addLog("Agent closed"); }