//기차역이름 코드 정보 public static void AcctoTrainSttnList(int city, string cityName) { results = ""; try { results = File.ReadAllText(@"./dg_xml/TrainSttnList_" + cityName + ".xml"); XElement TrainSttn = XElement.Parse(results); Console.WriteLine("TrainSttnList.xml load 성공"); Console.WriteLine(TrainSttn); //도시 코드 저장 foreach (var item in TrainSttn.Descendants("item")) { string tempNodeid = item.Element("nodeid").Value; string tempNodename = item.Element("nodename").Value; Train_D td = new Train_D() { nodeid = tempNodeid, nodename = tempNodename }; TrainSttnCode.Add(td); } //확인용 foreach (var item in TrainSttnCode) { Console.WriteLine(item.nodename + item.nodeid); } } catch (Exception ex) { string url = "http://openapi.tago.go.kr/openapi/service/TrainInfoService/getCtyAcctoTrainSttnList"; // URL url += "?ServiceKey=" + "0IL6R0F8vitdpbkttdCEX3Uxse07CQ1RRK3plz%2BdAkBSYkIESNMfTtVmQk%2BPUDXLPQfvB3iGXJYvPOS2brP4gQ%3D%3D"; // Service Key url += "&numOfRows=104"; url += "&pageNo=1"; url += "&cityCode=" + city; request(url);//api 호출 XElement TrainSttn = XElement.Parse(results); File.WriteAllText(@"./dg_xml/TrainSttnList_" + cityName + ".xml", TrainSttn.ToString()); //Console.WriteLine(TrainSttn); writeLog(ex.Message); writeLog(ex.StackTrace); AcctoTrainSttnList(city, cityName); // 다시 실행 } }
//지역 코드 정보 public static void getCtyCodeList() { results = ""; try { results = File.ReadAllText(@"./dg_xml/ctyCode.xml"); Console.WriteLine("ctyCode.xml load 성공"); XElement ctyCode = XElement.Parse(results); //도시 코드 저장 foreach (var item in ctyCode.Descendants("item")) { int tempCityCode = int.Parse(item.Element("citycode").Value); string tempCtiyName = item.Element("cityname").Value; Train_D td = new Train_D() { citycode = tempCityCode, cityname = tempCtiyName }; trainGradeCode.Add(td); } //확인용 foreach (var item in trainGradeCode) { Console.WriteLine(item.citycode); } } catch (Exception ex) { string url = "http://openapi.tago.go.kr/openapi/service/TrainInfoService/getCtyCodeList"; // URL url += "?ServiceKey=" + "0IL6R0F8vitdpbkttdCEX3Uxse07CQ1RRK3plz%2BdAkBSYkIESNMfTtVmQk%2BPUDXLPQfvB3iGXJYvPOS2brP4gQ%3D%3D"; // Service Key request(url); //api 호출 XElement ctyCode = XElement.Parse(results); File.WriteAllText(@"./dg_xml/ctyCode.xml", ctyCode.ToString()); writeLog(ex.Message); writeLog(ex.StackTrace); getCtyCodeList();//다시 호출 } }
//모든 기차역 코드 정보 public static void getAllsttnCode() { results = ""; try { results = File.ReadAllText(@"./AllTrainCode.xml"); Console.WriteLine("AllTrainCode.xml load 성공"); XElement AllsttnCode = XElement.Parse(results); //도시 코드 저장 foreach (var item in AllsttnCode.Descendants("item")) { string tempNodeid = item.Element("nodeid").Value; string tempNodename = item.Element("nodename").Value; Train_D td = new Train_D() { nodeid = tempNodeid, nodename = tempNodename }; AllTrainCode.Add(td); } //확인용 foreach (var item in AllTrainCode) { Console.WriteLine(item.nodename); } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); System.Windows.Forms.MessageBox.Show(ex.StackTrace); writeLog(ex.Message); writeLog(ex.StackTrace); throw; } }
//출발도착정보 기차종류 ALL,선택 (오버로딩) public static void ShowInfo(string depPlaceId, string arrPlaceId, List <string> gradeCode) { try { results = ""; TrainInfo.Clear(); foreach (var it in gradeCode) //gradeCode 기차코드 별 API 불러옴 { string url = "http://openapi.tago.go.kr/openapi/service/TrainInfoService/getStrtpntAlocFndTrainInfo"; // URL url += "?ServiceKey=" + "0IL6R0F8vitdpbkttdCEX3Uxse07CQ1RRK3plz%2BdAkBSYkIESNMfTtVmQk%2BPUDXLPQfvB3iGXJYvPOS2brP4gQ%3D%3D"; // Service Key url += "&numOfRows=104"; url += "&depPlaceId=" + depPlaceId; url += "&arrPlaceId=" + arrPlaceId; url += "&depPlandTime=" + DateTime.Now.ToString("yyyyMMdd"); if (it != "ALL")//ALL 아니면 기차코드 URL 입력 { url += "&trainGradeCode=" + it; } Console.WriteLine(url); request(url);//api 호출 XElement ShowInfo = XElement.Parse(results); //열차정보 저장 foreach (var item in ShowInfo.Descendants("item")) { string tmpTraingradename = item.Element("traingradename").Value; //차량종류명 DateTime tmpDepplandtime = DateTime.ParseExact(item.Element("depplandtime").Value, "yyyyMMddHHmmss", null); //차량출발시간 DateTime tmpArrplandtime = DateTime.ParseExact(item.Element("arrplandtime").Value, "yyyyMMddHHmmss", null); //도착시간 string tmpDepplacename = item.Element("depplacename").Value; //출발지 string tmpArrplacename = item.Element("arrplacename").Value; //도착지 string tmpTrainno = item.Element("trainno").Value; //열차번호 string tmpNodename = item.Element("arrplacename").Value + "역"; //도착지 //도착시간 구하는 string test = DateTime.Now.ToString("yyyyMMddHHmmss"); DateTime now = DateTime.ParseExact(test, "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture); TimeSpan timeSpan = tmpArrplandtime - now; int timeHoure = timeSpan.Hours; int timeMinutes = timeSpan.Minutes; Console.WriteLine("시간 :" + item.Element("arrplandtime")); Train_D td = new Train_D() { traingradename = tmpTraingradename, depplandtime = item.Element("depplandtime").Value.Substring(4, 2) + "월" + item.Element("depplandtime").Value.Substring(6, 2) + "일 " + item.Element("depplandtime").Value.Substring(8, 2) + ":" + item.Element("depplandtime").Value.Substring(10, 2), arrplandtime = item.Element("arrplandtime").Value.Substring(4, 2) + "월" + item.Element("arrplandtime").Value.Substring(6, 2) + "일 " + item.Element("arrplandtime").Value.Substring(8, 2) + ":" + item.Element("arrplandtime").Value.Substring(10, 2), depplacename = tmpDepplacename, arrplacename = tmpArrplacename, trainno = tmpTrainno, nodename = tmpNodename, outTime = (timeHoure * 60) + timeMinutes + "분", timeCheck = (timeHoure * 60) + timeMinutes }; if ((timeHoure * 60) + timeMinutes >= 0) { if ((timeHoure * 60) + timeMinutes == 0) { td.outTime = "도착"; } TrainInfo.Add(td); } //Console.WriteLine(ShowInfo); url = ""; //초기화 } } Console.WriteLine(TrainInfo[0].depplacename); save(); // xml 정보저장 writeLog("출발역:" + TrainInfo[0].depplacename + "_도착역:" + TrainInfo[0].arrplacename); TrainInfo = TrainInfo.OrderBy(item => item.timeCheck).ToList(); //확인용 foreach (var item in TrainInfo) { Console.WriteLine(item.timeCheck); } } catch (Exception ex) { writeLog(ex.Message); writeLog(ex.StackTrace); } }
//출발도착정보 ALL 사용안하는중 public static void ShowInfo(string depPlaceId, string arrPlaceId) { results = ""; TrainInfo.Clear(); string url = "http://openapi.tago.go.kr/openapi/service/TrainInfoService/getStrtpntAlocFndTrainInfo"; // URL url += "?ServiceKey=" + "0IL6R0F8vitdpbkttdCEX3Uxse07CQ1RRK3plz%2BdAkBSYkIESNMfTtVmQk%2BPUDXLPQfvB3iGXJYvPOS2brP4gQ%3D%3D"; // Service Key url += "&numOfRows=104"; url += "&depPlaceId=" + depPlaceId; url += "&arrPlaceId=" + arrPlaceId; url += "&depPlandTime=" + DateTime.Now.ToString("yyyyMMdd"); request(url);//api 호출 XElement ShowInfo = XElement.Parse(results); //도시 코드 저장 foreach (var item in ShowInfo.Descendants("item")) { string tmpTraingradename = item.Element("traingradename").Value; //차량종류명 DateTime tmpDepplandtime = DateTime.ParseExact(item.Element("depplandtime").Value, "yyyyMMddHHmmss", null); //차량출발시간 DateTime tmpArrplandtime = DateTime.ParseExact(item.Element("arrplandtime").Value, "yyyyMMddHHmmss", null); //도착시간 string tmpDepplacename = item.Element("depplacename").Value; //출발지 string tmpArrplacename = item.Element("arrplacename").Value; //도착지 string tmpTrainno = item.Element("trainno").Value; //열차번호 string tmpNodename = item.Element("arrplacename").Value + "역"; //도착지 //도착시간 구하는 string test = DateTime.Now.ToString("yyyyMMddHHmmss"); DateTime now = DateTime.ParseExact(test, "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture); TimeSpan timeSpan = tmpArrplandtime - now; // int timeHoure = timeSpan.Hours; int timeMinutes = timeSpan.Minutes; Console.WriteLine("시간 :" + item.Element("arrplandtime")); Train_D td = new Train_D() { traingradename = tmpTraingradename, depplandtime = item.Element("depplandtime").Value.Substring(4, 2) + "월" + item.Element("depplandtime").Value.Substring(6, 2) + "일 " + item.Element("depplandtime").Value.Substring(8, 2) + ":" + item.Element("depplandtime").Value.Substring(10, 2), arrplandtime = item.Element("arrplandtime").Value.Substring(4, 2) + "월" + item.Element("arrplandtime").Value.Substring(6, 2) + "일 " + item.Element("arrplandtime").Value.Substring(8, 2) + ":" + item.Element("arrplandtime").Value.Substring(10, 2), // depplandtime = tmpDepplandtime.Substring(8, 2) + ":" + tmpDepplandtime.Substring(10, 2), //arrplandtime = tmpArrplandtime.Substring(8, 2) + ":" + tmpArrplandtime.Substring(10, 2), depplacename = tmpDepplacename, arrplacename = tmpArrplacename, trainno = tmpTrainno, nodename = tmpNodename, outTime = (timeHoure * 60) + timeMinutes + "분", timeCheck = (timeHoure * 60) + timeMinutes }; if ((timeHoure * 60) + timeMinutes >= 0) { if ((timeHoure * 60) + timeMinutes == 0) { td.outTime = "도착"; } TrainInfo.Add(td); } //Console.WriteLine(ShowInfo); File.WriteAllText(@"./dg_xml/ShowInfo[" + depPlaceId + "][" + arrPlaceId + "][" + DateTime.Now.ToString("yyyyMMdd") + "].xml", ShowInfo.ToString()); } TrainInfo = TrainInfo.OrderBy(x => x.timeCheck).ToList(); //확인용 foreach (var item in TrainInfo) { Console.WriteLine(item.timeCheck); } }