Algady Khalefa

Algady Khalefa

  • NA
  • 18
  • 775

filling RichtextBox from a list

Apr 28 2017 12:29 PM
Hi, I'm working on a project where I need to read weather data from openweathermap, for now I've managed to read data successfuly to my list but I'm not able to do any usefull steps with my data such as write it in a RichtextBox for example!
please note:
when I put a break point it line 70 and run it I can find that object w (line 68) has all data which read as expected.
my question is how can I read data from that list and write it for example to richtextbox?
please help and sorry for my english.
  1. namespace Weather_app  
  2. {  
  3.     public partial class Form1 : Form  
  4.     {  
  5.         public Form1()  
  6.         {  
  7.             InitializeComponent();  
  8.         }  
  9.           
  10.   
  11.              private static string AppID = "df2c579492931fb3df887b04edead343";  
  12.   
  13.         public static IEnumerable> xEl { getprivate set; }  
  14.   
  15.         public static async Task> GetWeather(string location)  
  16.         {  
  17.   
  18.             string url = string.Format  
  19.                 ("http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&type=accurate&mode=xml&units=metric&cnt=5&appid={1}",  
  20.                 "Leeds", AppID);  
  21.             using (HttpClient client = new HttpClient())  
  22.             {  
  23.                 try  
  24.                 {  
  25.                     string response = await client.GetStringAsync(url);  
  26.                     if (!(response.Contains("message") && response.Contains("cod")))  
  27.                     {  
  28.                         XElement xEl = XElement.Load(new System.IO.StringReader(response));  
  29.                         return GetWeatherInfo(xEl);  
  30.                     }  
  31.                     else  
  32.                     {  
  33.   
  34.                         return new List();  
  35.   
  36.                     }  
  37.                 }  
  38.                 catch (HttpRequestException)  
  39.                 {  
  40.                     return new List();  
  41.                 }  
  42.             }  
  43.         }  
  44.   
  45.   
  46.   
  47.         public static List GetWeatherInfo(XElement xEl)  
  48.         {  
  49.   
  50.             IEnumerable w = xEl.Descendants("time").Select((el) => new WeatherDetails  
  51.   
  52.             {  
  53.                 Humidity = el.Element("humidity").Attribute("value").Value + "%",  
  54.                   
  55.                 MaxTemperature = el.Element("temperature").Attribute("max").Value + "°",  
  56.                 MinTemperature = el.Element("temperature").Attribute("min").Value + "°",  
  57.                 Temperature = el.Element("temperature").Attribute("day").Value + "°",  
  58.                 Weather = el.Element("symbol").Attribute("name").Value,  
  59.                 WeatherDay = DayOfTheWeek(el),  
  60.                 WeatherIcon = WeatherIconPath(el),  
  61.                 WindDirection = el.Element("windDirection").Attribute("name").Value,  
  62.                 WindSpeed = el.Element("windSpeed").Attribute("mps").Value + "mps"  
  63.                  
  64.             });  
  65.   
  66.               
  67.   
  68.             return w.ToList();  
  69.              
  70.         }  
  71.   
  72.         
  73.   
  74.         private static string DayOfTheWeek(XElement el)  
  75.         {  
  76.             DayOfWeek dW = Convert.ToDateTime(el.Attribute("day").Value).DayOfWeek;  
  77.             return dW.ToString();  
  78.         }  
  79.   
  80.         private static string WeatherIconPath(XElement el)  
  81.         {  
  82.             string symbolVar = el.Element("symbol").Attribute("var").Value;  
  83.             string symbolNumber = el.Element("symbol").Attribute("number").Value;  
  84.             string dayOrNight = symbolVar.ElementAt(2).ToString(); // d or n  
  85.             return String.Format("WeatherIcons/{0}{1}.png", symbolNumber, dayOrNight);  
  86.         }  
  87.     
  88.     }  
  89. }  
 

Answers (2)