C#基于豆瓣API查询图书信息

首先创建一个bookinfo的类:
 class bookinfo
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _author;
        public string Author
        {
            get { return _author; }
            set { _author = value; }
        }
        private string _imageurl;
        public string Imageurl
        {
            get { return _imageurl; }
            set { _imageurl = value; }
        }
        private string _summary;
        public string Summary
        {
            get { return _summary; }
            set { _summary = value; }
        }
        private string _isbn;
        public string Isbn
        {
            get { return _isbn; }
            set { _isbn = value; }
        }
        private string _pages;
        public string Pages
        {
            get { return _pages; }
            set { _pages = value; }
        }
        private string _price;
        public string Price
        {
            get { return _price; }
            set { _price = value; }
        }
        private string _publisher;
        public string Publisher
        {
            get { return _publisher; }
            set { _publisher = value; }
        }
然后在主程序中调用bookinfo类:
 HttpWebRequest myRequest = null;
            HttpWebResponse myHttpResponse = null;
            string doubanurl = "http://api.douban.com/book/subject/isbn/";
            doubanurl = doubanurl + textBox10.Text + "?";
            myRequest = (HttpWebRequest)WebRequest.Create(doubanurl);
            myHttpResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myHttpResponse.GetResponseStream());
            string xmldetail = reader.ReadToEnd();
            reader.Close();
            myHttpResponse.Close();
            bookinfo books = new bookinfo();
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(xmldetail);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
            nsmgr.AddNamespace("db", "http://www.w3.org/2005/Atom");
            XmlElement root = xml.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("/db:entry", nsmgr);
            foreach (XmlNode nodeData in nodes)
            {
                foreach (XmlNode childnode in nodeData.ChildNodes)
                {
                    string str = childnode.Name;
                    switch (str)
                    {
                        case "title":
                            books.Name = childnode.InnerText;
                            break;
                        case "link":
                            if (childnode.Attributes[1].Value == "image")
                            {
                                books.Imageurl = childnode.Attributes[0].Value;
                            }
                            break;
                        case "summary":
                            books.Summary = childnode.InnerText;
                            break;
                        case "db:attribute":
                            {
                                switch (childnode.Attributes[0].Value)
                                {
                                    case "isbn13":
                                        books.Isbn = childnode.InnerText;
                                        break;
                                    case "pages":
                                        books.Pages = childnode.InnerText;
                                        break;
                                    case "author":
                                        books.Author = childnode.InnerText;
                                        break;
                                    case "price":
                                        books.Price = childnode.InnerText;
                                        break;
                                    case "publisher":
                                        books.Publisher = childnode.InnerText;
                                        break;
                                    case "pubdate":
                                        books.Pubdate = childnode.InnerText;
                                        break;
                                }//end switch
                                break;
                            }
                    }//end switch
                }//end foreach
                textBox1.Text = "书名:" + books.Name;
                textBox2.Text = "作者:" + books.Author;
                textBox3.Text = "封面:" + books.Imageurl;
                textBox4.Text = "ISBN:" + books.Isbn;
                textBox5.Text = "页数:" + books.Pages;
                textBox6.Text = "价格:" + books.Price;
                textBox7.Text = "出版社:" + books.Publisher;
                textBox8.Text = "出版日期:" + books.Pubdate;
                textBox9.Text = "简介:" + books.Summary;
                pictureBox1.WaitOnLoad = false;
                pictureBox1.LoadAsync(books.Imageurl);//使用异步加载图书封面照片 

回头写一个百度音乐的抓取类,把以前的重写一遍,哈哈 

你可能感兴趣的:(.net,C#,图书,豆瓣)