Ejemplo n.º 1
0
        private static void InsertReviewData(XmlTextWriter writer, BookReview review)
        {
            writer.WriteStartElement("review");

            if (review.BookReviewDate != null)
            {
                writer.WriteElementString("date", review.BookReviewDate.Value.ToString("dd-MMM-yyyy"));
            }

            writer.WriteElementString("content", review.BookReviewContents);

            writer.WriteStartElement("book");

            writer.WriteElementString("title", review.Book.BookTitle);

            if (review.Book.Authors.Count > 0)
            {
                var authorNames = review.Book.Authors.Select(a => a.AuthorName).OrderBy(n => n);
                writer.WriteElementString("authors", string.Join(", ", authorNames));
            }

            if (review.Book.BookISBN != null)
            {
                writer.WriteElementString("isbn", review.Book.BookISBN);
            }

            if (review.Book.BookWebSite != null)
            {
                writer.WriteElementString("url", review.Book.BookWebSite);
            }

            writer.WriteEndElement();

            writer.WriteEndElement();
        }
Ejemplo n.º 2
0
        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();
            }
        }