Created
May 25, 2019 09:28
-
-
Save karthik20522/9e3d0699b36aadad46a486e57184b330 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Weather { | |
public string city; | |
public int curr_temp; | |
public string curr_text; | |
public int curr_icon; | |
public List < Forecast > forecast = new List < Forecast > (); | |
} | |
public class Forecast { | |
public string day_date; | |
public string day_text; | |
public int day_icon; | |
public int day_htemp; | |
public int day_ltemp; | |
} | |
//code | |
Weather _weather; | |
if (_weather == null) { | |
_weather = new Weather(); | |
string _location = code; | |
string _metric = "0"; | |
string _url = string.Format("http://wwwa.accuweather.com/adcbin/forecastfox/weather_data.asp?location={0}&metric={1}", _location, _metric); | |
string _xml = Utils.DownloadWebPage(_url); | |
var _xmlDocument = new XmlDocument(); | |
_xmlDocument.LoadXml(_xml); | |
var _mgr = new XmlNamespaceManager(_xmlDocument.NameTable); | |
_mgr.AddNamespace("pf", _xmlDocument.DocumentElement.NamespaceURI); | |
_weather.city = _xmlDocument.SelectSingleNode("/pf:adc_database/pf:local/pf:city", _mgr).InnerText; | |
_weather.curr_temp = Convert.ToInt32(_xmlDocument.SelectSingleNode("/pf:adc_database/pf:currentconditions/pf:temperature", _mgr).InnerText); | |
_weather.curr_text = _xmlDocument.SelectSingleNode("/pf:adc_database/pf:currentconditions/pf:weathertext", _mgr).InnerText; | |
_weather.curr_icon = Convert.ToInt32(_xmlDocument.SelectSingleNode("/pf:adc_database/pf:currentconditions/pf:weathericon", _mgr).InnerText); | |
var _xmlNodeList = _xmlDocument.SelectNodes("/pf:adc_database/pf:forecast/pf:day", _mgr); | |
int _day = _xmlNodeList.Count; | |
int i = 0; | |
foreach(var _dayItem in _xmlNodeList) { | |
var _forecast = new Forecast(); | |
_forecast.day_date = _dayItem["obsdate"].InnerXml; | |
_forecast.day_text = _dayItem.SelectSingleNode("pf:daytime", _mgr)["txtshort"].InnerXml; | |
_forecast.day_icon = Convert.ToInt32(_dayItem.SelectSingleNode("pf:daytime", _mgr)["weathericon"].InnerXml); | |
_forecast.day_htemp = Convert.ToInt32(_dayItem.SelectSingleNode("pf:daytime", _mgr)["hightemperature"].InnerXml); | |
_forecast.day_ltemp = Convert.ToInt32(_dayItem.SelectSingleNode("pf:daytime", _mgr)["lowtemperature"].InnerXml); | |
_weather.forecast.Add(_forecast); | |
i++; | |
// 5 day forecast | |
if (i == 5) break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment