Programing (프로그래밍)/WPF & C# (C Sharp)
XML 파싱 - XPATH ( parsing / 기상청 / weather / 날씨 )
insurang
2018. 1. 10. 13:42
728x90
반응형
XML 파싱 - XPATH ( parsing )
XML 파싱 - XPATH ( parsing ) 테스트 - 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | private void xmlParsing(string strRead) { /* 신동네예보정보조회서비스 1.4*/ XmlDocument XmlDoc = new XmlDocument(); XmlDoc.LoadXml(strRead); // File = XmlDoc.Load, String = XmlDoc.LoadXml XmlNodeList tagNodes = XmlDoc.SelectNodes("/response/body/items/item"); // 노드의 정렬된 컬렉션 ( //Tag 로 된 노드들만 가져옴) foreach (XmlNode tagNode in tagNodes) { XmlNode baseDate = tagNode.SelectSingleNode("baseDate"); XmlNode baseTime = tagNode.SelectSingleNode("baseTime"); XmlNode category = tagNode.SelectSingleNode("category"); MessageBox.Show(baseDate.InnerText + "," + baseTime.InnerText + "," + category.InnerText); // Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { lbl.Content += n.LocalName; })); } } | cs |
XML과 XPATH 를 이용하여 읽어들인 자료이다.
[원문 및 참고자료]
XML 파싱 - XPATH ( parsing ) 테스트 - 기상청
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.IO; using System.Net; using System.Text; using System.Windows; using System.Xml; private void xmlParsing(string strRead) { /* 기상청자료*/ // 자료읽기 strRead = "http://www.weather.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108"; XmlDocument XmlDoc = new XmlDocument(); XmlDoc.Load(strRead); // File = XmlDoc.Load, String = XmlDoc.LoadXml XmlNodeList Nodes = XmlDoc.SelectNodes("//channel/item/description/body/location[8]"); // location 8번째 node (대전) foreach (XmlNode Node in Nodes) { XmlNode city = Node.SelectSingleNode("city"); MessageBox.Show(city.InnerText); } XmlNodeList Nodes2 = XmlDoc.SelectNodes("//channel/item/description/body/location[8]/data"); // location 8번째 node (대전)의 하위data node string data2 = ""; foreach (XmlNode Node in Nodes2) { XmlNode tmEf = Node.SelectSingleNode("tmEf"); XmlNode wf = Node.SelectSingleNode("wf"); data2 += tmEf.InnerText + wf.InnerText + "\n"; // Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { lbl.Content += n.LocalName; })); } MessageBox.Show(data2); } | cs |
기상청자료 (xml) 을 기준으로 파싱해봤다.
http://www.weather.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108
728x90
반응형