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); } }