도서 검색 프로그램
네이버 OpenAPI 으로 검색을 요청 해당 XML을 파싱하여 프로그램에 보여줄 수 있게 하였다.
Book 클래스는 아래와 같은 디자인으로 되어있다.
class Book { public string Bname { get; private set; } public string Publisher { get; private set; } public string Author { get; private set; } public int Price { get; private set; } public int DisPrice { get; private set; } public string Link { get; private set; } public string Desc { get; private set; } public Book(string _bname, string _publisher, string _author, int _price, string _link, string _desc) { Bname = _bname; Publisher = _publisher; Author = _author; Price = _price; Link = _link; Desc = _desc; } public override string ToString() { return this.Bname; } |
위그림을 통해 네이버에서 제공하는 검색api를 사용하여 xml 로 나타난 목록을 확인 할 수 있다.
BookSearch 클래스
static class BookSearch { static ArrayList bl = new ArrayList(); public static ArrayList Naver(string booksname) { bl.Clear(); string bname = null; string publisher = null; string author = null; string link = null; int price = 0; int disprice = 0; string addr = string.Format("http://openapi.naver.com/search?key=??????????????????&query={0}&target=book&display=100&start=1", HttpUtility.UrlEncode(booksname, Encoding.GetEncoding("utf-8"))); //???????????구간에 해당 사이트에서 발급받은 key값을 넣어준다. XmlDocument xd = new XmlDocument(); xd.Load(addr); XmlNode node = xd.SelectSingleNode("rss"); XmlNode xnode = node.SelectSingleNode("channel"); XmlNode cnode; XmlNodeList xnl = xnode.SelectNodes("item"); foreach (XmlNode xmlnode in xnl) { xnode = xmlnode.SelectSingleNode("title"); bname = ConvertString(xnode.InnerText); xnode = xmlnode.SelectSingleNode("author"); author = xnode.InnerText; xnode = xmlnode.SelectSingleNode("price"); price = int.Parse(xnode.InnerText); xnode = xmlnode.SelectSingleNode("discount"); try { disprice = int.Parse(xnode.InnerText); } catch { disprice = price; } xnode = xmlnode.SelectSingleNode("publisher"); publisher = xnode.InnerText; cnode = xmlnode.SelectSingleNode("description"); link = ConvertString(cnode.InnerText); cnode = xmlnode.SelectSingleNode("description"); string desc = ConvertString(cnode.InnerText); bl.Add(new Book(bname,publisher,author,price,link,desc)); } return bl; } private static string ConvertString(string p) { int i = 0; int j = 0; while ((i = p.IndexOf("<")) != -1) { j = p.IndexOf(">"); p = p.Remove(i, j - i + 1); } return p; } } |
'Programing > C#&.Net' 카테고리의 다른 글
D Day 프로그램 (0) | 2016.11.30 |
---|---|
계산기 예제 (0) | 2016.11.30 |
노트북 블루투스 + 안드로이드 폰 연동 (0) | 2016.11.30 |
speech To Text (0) | 2016.11.30 |
회원관리 연습 (0) | 2016.11.30 |