본문 바로가기

   
Programming/XML

네이버 api 이용 도서 검색하기

반응형



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.Web;

using System.Net;

using System.IO;

 

namespace WinFormEx

{

       public partial class Ex05 : Form

       {

             public Ex05()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    SearchBook();

             }

 

             private void textBox1_KeyDown(object sender, KeyEventArgs e)

             {

                    if (e.KeyCode == Keys.Enter)

                    {

                           SearchBook();

                    }

             }

 

             private void SearchBook()

             {

                    try

                    {

 

                           panel2.Controls.Clear();//초기화

 

                           //http://openapi.naver.com/search?key=7a6113b7791ce0313cb577f437131e3d&query=검색어&display=50&start=1&target=book

 

                           string search = textBox1.Text.Trim();

 

                           XmlDocument doc = new XmlDocument();

                           doc.Load(string.Format("http://openapi.naver.com/search?key=7a6113b7791ce0313cb577f437131e3d&query={0}&display=50&start=1&target=book", search));

 

                           //MessageBox.Show(doc.OuterXml);

                           //richTextBox1.Text = doc.OuterXml;

 

 

 

                           XmlNodeList result = doc.SelectNodes("//item");

                           int x = 10, y = 10;

 

                           foreach (XmlElement item in result)

                           {

                                 //결과 <item> 1개당 -> BookItem유저컨트롤 1

                                 BookItem book = new BookItem();

 

                                 book.labelTitle.Text = item.FirstChild.InnerText.Replace("<strong>", "").Replace("</strong>", "");//제목

 

                                 book.groupBox1.Text = item.FirstChild.InnerText.Replace("< strong >", "").Replace("</strong>", "");

 

                                 //book.labelLink.Text

 

                                 book.labelAuthor.Text = item.ChildNodes[3].InnerText;//저자

 

                                 //"90000" -> "90,000"

                                 string price = string.Format("{0:N0}", int.Parse(item.ChildNodes[4].InnerText));

                                 book.labelPrice.Text = price;//가격

 

                                 book.labelISBN.Text = item.ChildNodes[8].InnerText;//ISBN

                                 book.labelPublisher.Text = item.ChildNodes[6].InnerText;

 

                                 string date = item.ChildNodes[7].InnerText;//20120501

                                 book.labelPubdate.Text = string.Format("{0} {1} {2}", date.Substring(0, 4), date.Substring(4, 2), date.Substring(6, 2));

 

                                 //사진

                                 // - 윈폼은 URL주소 형식의 이미지 출력 X

                                 //Bitmap img = new Bitmap(item.ChildNodes[2].InnerText);

                                 WebRequest request = WebRequest.Create(item.ChildNodes[2].InnerText);

                                 WebResponse response = request.GetResponse();

 

                                 Stream rs = response.GetResponseStream();

 

                                 Bitmap img = new Bitmap(rs);

 

                                 book.pictureBox1.Image = img;

 

 

                                 //링크 걸기

                                 book.labelLink.Tag = item.ChildNodes[1].InnerText;

                                 book.labelLink.LinkClicked += new LinkLabelLinkClickedEventHandler(labelLink_LinkClicked);

 

                                 //BookItem Panel1의 자식으로 추가

                                 panel2.Controls.Add(book);

 

                                 //유저 컨트롤 위치 선정

                                 book.Location = new Point(x, y);

 

                                 y += 200;

                           }

 

                           panel2.Height = y + 200;//**

                           vScrollBar1.Maximum = y - 200;

 

                    }

 

                    catch { }

                   

             }

 

             void labelLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

             {

                    //브라우저 호출

                    System.Diagnostics.Process.Start("iexplore.exe", ((LinkLabel)sender).Tag.ToString());

             }

 

             private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)

             {

                    //vScrollBar1.Value -> Panel2 -Y좌표값

                    panel2.Location = new Point(0, -vScrollBar1.Value);

             }

 

             private void Ex05_Load(object sender, EventArgs e)

             {

 

             }

       }

}

 

 

반응형