예제 #1
0
        public void BooksComplexParse()
        {
            var books = this.GetBooksXml();

            foreach (var bookXml in books)
            {
                var title = bookXml.Element("title").Value;
                var book  = new Book {
                    Title = title
                };

                var authorsXml = bookXml.Elements("authors");
                var authors    = authorsXml.Select(authorXml => AuthorDao.GetOrCreateAuthor(this.Entities, authorXml.Value)).ToList();
                book.Authors = authors;

                this.BookSetNotRequiredProperty("ISBN", "isbn", bookXml, book);
                this.BookSetNotRequiredProperty("Price", "price", bookXml, book);
                this.BookSetNotRequiredProperty("Url", "web-site", bookXml, book);

                var reviewsXml = bookXml.Elements("reviews");
                var reviews    = new List <Review>();
                foreach (var reviewXml in reviewsXml)
                {
                    var    dateAttribute = reviewXml.Attribute("date");
                    string date          = null;
                    if (dateAttribute != null)
                    {
                        date = dateAttribute.Value;
                    }

                    var    authorAttribute = reviewXml.Attribute("author");
                    string author          = null;
                    if (authorAttribute != null)
                    {
                        author = authorAttribute.Value;
                    }


                    reviews.Add(ReviewDao.CreateReview(this.Entities, reviewXml.Value, date, author));
                }

                book.Reviews = reviews;

                this.Entities.Books.Add(book);
                this.Entities.SaveChanges();
            }
        }
예제 #2
0
        public void SimpleBooksParse()
        {
            var books = this.GetBooksXml();

            foreach (var bookXml in books)
            {
                var title = bookXml.Element("title").Value;
                var book  = new Book {
                    Title = title
                };

                var authorName = bookXml.Element("author").Value;
                var author     = AuthorDao.GetOrCreateAuthor(this.Entities, authorName);
                book.Authors.Add(author);

                this.BookSetNotRequiredProperty("ISBN", "isbn", bookXml, book);
                this.BookSetNotRequiredProperty("Price", "price", bookXml, book);
                this.BookSetNotRequiredProperty("Url", "web-site", bookXml, book);

                this.Entities.Books.Add(book);
                this.Entities.SaveChanges();
            }
        }