private static void ParseAndSaveBook(BookstoreEntities bookstoreContext, XPathNodeIterator iterator) { using (var scope = new TransactionScope()) { XPathNavigator currentNode = iterator.Current; XPathNavigator bookAuthorNode = currentNode.SelectSingleNode("author"); XPathNavigator bookTitleNode = currentNode.SelectSingleNode("title"); XPathNavigator bookIsbnNode = currentNode.SelectSingleNode("isbn"); XPathNavigator bookPriceNode = currentNode.SelectSingleNode("price"); XPathNavigator bookWebSiteNode = currentNode.SelectSingleNode("web-site"); string authorName = Utils.GetNodeValue(bookAuthorNode); if (authorName == null) { throw new XPathException("Book author is a required tag."); } string bookIsbn = Utils.GetNodeValue(bookIsbnNode); string bookTitle = Utils.GetNodeValue(bookTitleNode); if (bookTitle == null) { throw new XPathException("Book title is a required tag."); } string bookPriceAsString = Utils.GetNodeValue(bookPriceNode); decimal? bookPrice = null; if (bookPriceAsString != null) { bookPrice = decimal.Parse(bookPriceAsString); } string bookWebSite = Utils.GetNodeValue(bookWebSiteNode); var author = Utils.GetAuthor(bookstoreContext, bookAuthorNode); var book = new Book { BookTitle = bookTitle, BookISBN = bookIsbn, BookPrice = bookPrice, BookWebSite = bookWebSite }; book.Authors.Add(author); bookstoreContext.Books.Add(book); bookstoreContext.SaveChanges(); scope.Complete(); } }
public static Author GetAuthor(BookstoreEntities bookstoreContext, XPathNavigator node) { string authorName = Utils.GetNodeValue(node); var author = bookstoreContext.Authors.FirstOrDefault(a => a.AuthorName == authorName); if (author == null) { author = new Author { AuthorName = authorName }; bookstoreContext.Authors.Add(author); bookstoreContext.SaveChanges(); } return author; }
private static void ParseAndSaveBook(BookstoreEntities bookstoreContext, XPathNodeIterator iterator) { using (var scope = new TransactionScope()) { XPathNavigator currentNode = iterator.Current; XPathNavigator bookTitleNode = currentNode.SelectSingleNode("title"); XPathNavigator bookAuthorsNode = currentNode.SelectSingleNode("authors"); XPathNavigator bookWebSiteNode = currentNode.SelectSingleNode("web-site"); XPathNavigator bookReviewsNode = currentNode.SelectSingleNode("reviews"); XPathNavigator bookIsbnNode = currentNode.SelectSingleNode("isbn"); XPathNavigator bookPriceNode = currentNode.SelectSingleNode("price"); string bookTitle = Utils.GetNodeValue(bookTitleNode); if (bookTitle == null) { throw new XPathException("Book title is a required tag."); } var book = new Book(); book.BookTitle = bookTitle; if (bookAuthorsNode != null) { var authorsIterator = bookAuthorsNode.SelectChildren(XPathNodeType.Element); while (authorsIterator.MoveNext()) { XPathNavigator authorNode = authorsIterator.Current; var author = Utils.GetAuthor(bookstoreContext, authorNode); book.Authors.Add(author); } } string bookWebSite = Utils.GetNodeValue(bookWebSiteNode); book.BookWebSite = bookWebSite; string bookIsbn = Utils.GetNodeValue(bookIsbnNode); book.BookISBN = bookIsbn; string bookPriceAsString = Utils.GetNodeValue(bookPriceNode); decimal? bookPrice = null; if (bookPriceAsString != null) { bookPrice = decimal.Parse(bookPriceAsString); } book.BookPrice = bookPrice; bookstoreContext.Books.Add(book); bookstoreContext.SaveChanges(); if (bookReviewsNode != null) { var reviewsIterator = bookReviewsNode.SelectChildren(XPathNodeType.Element); while (reviewsIterator.MoveNext()) { var bookReview = new BookReview(); XPathNavigator reviewNode = reviewsIterator.Current; string reviewContents = Utils.GetNodeValue(reviewNode); bookReview.BookReviewContents = reviewContents; var reviewAuthorNode = reviewNode.SelectSingleNode("@author"); if (reviewAuthorNode != null) { var author = Utils.GetAuthor(bookstoreContext, reviewAuthorNode); bookReview.Author = author; } var reviewDateNode = reviewNode.SelectSingleNode("@date"); DateTime bookReviewDate = DateTime.Now; if (reviewDateNode != null) { bookReviewDate = Utils.GetDate(reviewDateNode); } bookReview.BookReviewDate = bookReviewDate; bookReview.Book = book; bookstoreContext.BookReviews.Add(bookReview); } bookstoreContext.SaveChanges(); } scope.Complete(); } }