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();
            }
        }
        private static void Main()
        {
            // The Bookstore.sql script is in the \Resources folder of the Models project.

            BookstoreEntities bookstoreContext = null;

            try
            {
                XPathDocument doc = new XPathDocument("../../Resources/test5.xml");
                XPathNavigator navigator = doc.CreateNavigator();
                XPathExpression expression = navigator.Compile("/catalog/book");
                XPathNodeIterator iterator = navigator.Select(expression);

                bookstoreContext = new BookstoreEntities();

                while (iterator.MoveNext())
                {
                    ParseAndSaveBook(bookstoreContext, iterator);
                }
            }
            catch (XPathException xpe)
            {
                Console.WriteLine(xpe.Message);
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
            }
            finally
            {
                if (bookstoreContext != null)
                {
                    bookstoreContext.Dispose();
                }
            }
        }
        private static void ProcessQuery(BookstoreEntities bookstoreContext, XPathNodeIterator iterator, XmlTextWriter writer)
        {
            writer.WriteStartElement("result-set");

            XPathNavigator currentNode = iterator.Current;

            var queryTypeNode = currentNode.SelectSingleNode("@type");
            string queryType = queryTypeNode.Value;

            IOrderedQueryable<BookReview> reviewsFound = null;

            if (queryType == "by-period")
            {
                var startDateNode = currentNode.SelectSingleNode("start-date");
                DateTime startDate = Utils.GetDate(startDateNode);

                var endDateNode = currentNode.SelectSingleNode("end-date");
                DateTime endDate = Utils.GetDate(endDateNode);

                reviewsFound =
                    from bookReview in bookstoreContext.BookReviews.Include(br => br.Book.Authors)
                    where bookReview.BookReviewDate >= startDate && bookReview.BookReviewDate <= endDate
                    orderby bookReview.BookReviewDate, bookReview.BookReviewContents
                    select bookReview;
            }
            else
            {
                var authorNameNode = currentNode.SelectSingleNode("author-name");
                string authorName = authorNameNode.Value;

                reviewsFound =
                    from bookReview in bookstoreContext.BookReviews.Include(br => br.Book.Authors)
                    where string.Compare(bookReview.Author.AuthorName, authorName, true) == 0
                    orderby bookReview.BookReviewDate, bookReview.BookReviewContents
                    select bookReview;
            }

            foreach (var review in reviewsFound)
            {
                InsertReviewData(writer, review);
            }

            writer.WriteEndElement();
        }
示例#4
0
    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 Main()
        {
            string bookTitle = null;
            string bookAuthor = null;
            string bookIsbn = null;

            using (XmlTextReader reader = new XmlTextReader("../../Resources/test1.xml"))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "title":
                                {
                                    bookTitle = reader.ReadElementString();
                                    break;
                                }
                            case "author":
                                {
                                    bookAuthor = reader.ReadElementString();
                                    break;
                                }
                            case "isbn":
                                {
                                    bookIsbn = reader.ReadElementString();
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                }
            }

            using (var bookstoreContext = new BookstoreEntities())
            {
                var booksList =
                    (from book in bookstoreContext.Books.Include(b => b.Authors)
                     where (bookTitle == null || string.Compare(book.BookTitle, bookTitle, true) == 0) &&
                     (bookAuthor == null || book.Authors.Any(a => string.Compare(a.AuthorName, bookAuthor, true) == 0)) &&
                     (bookIsbn == null || string.Compare(book.BookISBN, bookIsbn, true) == 0)
                     orderby book.BookTitle
                     select new
                     {
                         BookTitle = book.BookTitle,
                         ReviewsCount = book.BookReviews.Count()
                     }).ToList();

                int booksFound = booksList.Count;

                if (booksFound == 0)
                {
                    Console.WriteLine("Nothing found");
                }
                else
                {
                    Console.WriteLine("{0} book(s) found:", booksFound);

                    foreach (var book in booksList)
                    {
                        Console.WriteLine(
                            "{0} --> {1} reviews",
                            book.BookTitle,
                            book.ReviewsCount > 0 ? book.ReviewsCount.ToString() : "no");
                    }
                }
            }
        }
        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();
            }
        }
        private static void Main()
        {
            XPathDocument doc = new XPathDocument("../../Resources/performance-test.xml");
            XPathNavigator navigator = doc.CreateNavigator();
            XPathExpression expression = navigator.Compile("/review-queries/query");
            XPathNodeIterator iterator = navigator.Select(expression);

            string resultFileName = "../../Resources/performance-test-results.xml";
            Encoding encoding = Encoding.GetEncoding("windows-1251");

            using (XmlTextWriter writer = new XmlTextWriter(resultFileName, Encoding.UTF8))
            {
                writer.Formatting = Formatting.Indented;
                writer.IndentChar = '\t';
                writer.Indentation = 1;

                writer.WriteStartDocument();
                writer.WriteStartElement("search-results");

                using (var bookstoreContext = new BookstoreEntities())
                {
                    while (iterator.MoveNext())
                    {
                        ProcessQuery(bookstoreContext, iterator, writer);
                    }
                }

                writer.WriteEndElement();

                writer.WriteEndDocument();
            }
        }