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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
@functions{
private static string WeatherService = "http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?listLatLon={0}&format=24+hourly&numDays=1";
private static string ZipcodeService = "http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdXMLclient.php?listZipCodeList={0}";
public class Temperature
{
public string Zip { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public int MaxTemp { get; set; }
public int MinTemp { get; set; }
public string Forecast { get; set; }
}
private static string GetCoordinates(string zipcode){
string serviceURL = string.Format(ZipcodeService, zipcode);
string dwml = string.Empty;
System.Net.WebClient webClient = new System.Net.WebClient();
dwml = webClient.DownloadString(serviceURL);
System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(dwml);
var coordinates = xdoc.Descendants("latLonList").FirstOrDefault();
if(coordinates == null)
return string.Empty;
else
return coordinates.Value;
}
private static Temperature GetWeather(string zipcode){
string coordinates = GetCoordinates(zipcode);
if(coordinates == string.Empty){
return new Temperature();
}
string serviceURL = string.Format(WeatherService, coordinates);
string dwml = string.Empty;
System.Net.WebClient webClient = new System.Net.WebClient();
dwml = webClient.DownloadString(serviceURL);
return XmlToTemperature(dwml, coordinates, zipcode);
}
private static Temperature XmlToTemperature(string xml, string coordinates, string zipcode){
System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(xml);
var maxTemp = from t in xdoc.Descendants("temperature").Elements("value")
where t.Parent.Attribute("type").Value == "maximum"
select t;
var minTemp = from t in xdoc.Descendants("temperature").Elements("value")
where t.Parent.Attribute("type").Value == "minimum"
select t;
var max = maxTemp.FirstOrDefault().Value;
var min = minTemp.FirstOrDefault().Value;
string forecast = xdoc.Descendants("weather-conditions").Attributes("weather-summary").FirstOrDefault().Value;
string[] lonlat = coordinates.Split(',');
Temperature temp = new Temperature();
temp.MaxTemp = Convert.ToInt32(max);
temp.MinTemp = Convert.ToInt32(min);
temp.Zip = zipcode;
temp.Latitude = lonlat[0];
temp.Longitude = lonlat[1];
temp.Forecast = forecast;
return temp;
}
} |