Inheritance: Entity
Exemplo n.º 1
0
        public List<Book> BookParse(string xmlFile)
        {
            List<Book> bl = new List<Book>();

            XNamespace d = @"http://www.w3.org/2005/Atom";
            XNamespace db = @"http://www.douban.com/xmlns/";
            XNamespace gd = @"http://schemas.google.com/g/2005";
            XNamespace opensearch = @"http://a9.com/-/spec/opensearchrss/1.0/";

            XDocument doc = XDocument.Parse(xmlFile);
            foreach (XElement entry in doc.Descendants(d + "entry"))
            {
                Book book = new Book();
                foreach (XElement element in entry.Descendants(d + "title"))
                {
                    book.Title = element.Value;
                }

                foreach (XElement element in entry.Descendants(d + "summary"))
                {
                    book.Summary = element.Value;
                }

                foreach (XElement element in entry.Descendants(d + "link"))
                {
                    if (element.Attribute("rel").Value == "image")
                    {
                        book.Imageurl = element.Attribute("href").Value;
                    }
                    else if (element.Attribute("rel").Value == "alternate")
                    {
                        book.Weblink = element.Attribute("href").Value;
                    }
                }

                //对于加了冒号前缀的元素,使用下列代码
                foreach (XElement element in entry.Descendants(db + "attribute"))
                {
                    switch (element.Attribute("name").Value)
                    {
                        case "isbn13":
                            book.Isbn = element.Value;
                            break;
                        case "pages":
                            book.Pages = element.Value;
                            break;
                        case "author":
                            book.Author.Add(element.Value);
                            break;
                        case "price":
                            book.Price = element.Value;
                            break;
                        case "publisher":
                            book.Publisher = element.Value;
                            break;
                        case "pubdate":
                            book.Pubdate = element.Value;
                            break;
                        case "author-intro":
                            book.AuthorIntro = element.Value;
                            break;
                        case "binding":
                            book.Binding = element.Value;
                            break;
                        case "translator":
                            book.Translator.Add(element.Value);
                            break;
                        case "subtitle":
                            book.Subtitle = element.Value;
                            break;
                    }
                }

                foreach (XElement element in entry.Descendants(gd + "rating"))
                {
                    book.Rating.Average= element.Attribute("average").Value;
                    book.Rating.RatersNum = element.Attribute("numRaters").Value;
                }

                foreach (XElement element in entry.Descendants(db + "tag"))
                {
                    BookTag tag = new BookTag();
                    tag.Tag = element.Attribute("name").Value;
                    tag.Count = element.Attribute("count").Value;
                    book.Tags.Add(tag);
                }

                bl.Add(book);
            }

            return bl;
        }