Esempio n. 1
0
        public static void ImportXMLItem(string filePath)
        {
            BookReaderContext db = new BookReaderContext();

            XDocument xmlDoc = XDocument.Load(filePath);

            BookCollection bookCollection = new BookCollection
            {
                Id    = Guid.Parse(xmlDoc.Element("bookcollection").Attribute("id").Value),
                Title = (string)xmlDoc.Element("bookcollection").Element("title")
            };

            db.BookCollections.Add(bookCollection);

            var books = from bk in xmlDoc.Descendants("book")
                        select new Book
            {
                Id     = Guid.Parse(bk.Attribute("id").Value),
                Title  = (string)bk.Element("title"),
                Author = (string)bk.Element("author"),
                BookCollectionSequence = Int32.Parse(bk.Element("number").Value),
                BookCollection         = bookCollection,
                Chapters = (
                    from c in bk.Elements("chapters").Elements("chapter")
                    select new Chapter
                {
                    Id = Guid.Parse(c.Attribute("id").Value),
                    Number = Int32.Parse(c.Element("number").Value),
                    Title = (string)c.Element("title"),
                    PreText = (string)c.Element("subtitle"),
                    Verses = (
                        from v in c.Elements("verses").Elements("verse")
                        select new Verse
                    {
                        Id = Guid.Parse(v.Attribute("id").Value),
                        VerseText = (string)v.Element("text"),
                        VerseNumber = Int32.Parse(v.Element("number").Value)
                    }
                        ).ToArray()
                }).ToArray()
            };

            foreach (var book in books)
            {
                db.Books.Add(book);
            }

            db.SaveChanges();
        }
Esempio n. 2
0
        public static void ImportItem(string filePath)
        {
            BookReaderContext db = new BookReaderContext();

            BookCollection bookCollection = null;
            Book           book           = new Book();
            Chapter        chapter        = new Chapter();

            List <String> fileLines = System.IO.File.ReadAllLines(filePath).ToList();

            int  verseNumber            = 1;
            bool isBookCollection       = false;
            int  bookCollectionSequence = 1;

            foreach (String line in fileLines)
            {
                if (line.StartsWith("!BOOKCOLLECTIONDATA"))
                {
                    string[] collectionData = line.Split('|');

                    bookCollection = new BookCollection
                    {
                        Title = collectionData[1]
                    };

                    isBookCollection = true;
                    db.BookCollections.Add(bookCollection);
                }
                else if (line.StartsWith("!BOOKDATA"))
                {
                    string[] bookData = line.Split('|');

                    book = new Book
                    {
                        Title          = bookData[1],
                        Author         = bookData[2],
                        BookCollection = bookCollection
                    };

                    if (isBookCollection)
                    {
                        book.BookCollectionSequence = bookCollectionSequence;
                        bookCollectionSequence++;
                    }

                    db.Books.Add(book);
                }
                else if (line.StartsWith("!CHAPTERDATA"))
                {
                    string[] chapterData = line.Split('|');

                    chapter = new Chapter
                    {
                        Number  = String.IsNullOrEmpty(chapterData[1]) ? (int?)null : Int32.Parse(chapterData[1]),
                        Title   = chapterData[2],
                        PreText = chapterData[3],
                        Book    = book
                    };

                    verseNumber = 1;
                    db.Chapters.Add(chapter);
                    db.SaveChanges();
                }
                else
                {
                    Verse verse = new Verse
                    {
                        VerseNumber = verseNumber++,
                        VerseText   = line,
                        Chapter     = chapter
                    };

                    db.Verses.Add(verse);
                }
            }

            db.SaveChanges();
        }