Reputation: 462
im new in rest webservices and i try to understand how to work with them.
i define a http get request in C# like this:
static string HttpGet(string url)
{
HttpWebRequest req = WebRequest.Create(url)
as HttpWebRequest;
string result = null;
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
return result;
}
then i use HttpGet in a button to get xml data from THIS weather webservice (i put the txtOut to findout that my code work).
private void btnGet_Click(object sender, RoutedEventArgs e)
{
string test = HttpGet("http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml");
txtOut.Text = test;
}
its geting the whole xml from sending get request to that url: http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml
so my question is how can i save specific part of that xml in a variable? like minimum temperature that is in kalvin so i can convert it to Fahrenheit or Celsius.
help me please.
Upvotes: 0
Views: 2925
Reputation: 15364
You api also returns json, so you can use it like below (using Json.Net)
using (WebClient wc = new Webclient())
{
var json = wc.DownloadString("http://api.openweathermap.org/data/2.5/weather?q=London&mode=json");
var obj = JsonConvert.DeserializeObject<OpenWeatherMap.Root>(json);
}
public class OpenWeatherMap
{
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public double message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Main
{
public double temp { get; set; }
public int humidity { get; set; }
public double pressure { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
}
public class Wind
{
public double speed { get; set; }
public double gust { get; set; }
public int deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Root
{
public Coord coord { get; set; }
public Sys sys { get; set; }
public List<Weather> weather { get; set; }
public string @base { get; set; }
public Main main { get; set; }
public Wind wind { get; set; }
public Dictionary<string,double> rain { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
}
If you insist on using xml then you can use Linq2Xml + XPath
var xDoc = XDocument.Load("http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml");
var windSpeed = (double)xDoc.XPathSelectElement("//wind/speed").Attribute("value");
or
var temp = (double)xDoc.Root.Element("temperature").Attribute("value");
Upvotes: 7
Reputation: 526
You could get json response and convert to dynamic obj using Newtonsoft.Json json converot or I believe any other convertor which could convert json to dynamic. This will help you to avoid parsing errors if scheme of returned object be changed.
using (WebClient wc = new Webclient())
{
var json = wc.DownloadString("http://api.openweathermap.org/data/2.5/weather?q=London&mode=json");
dynamic jsonResult = JsonConvert.DeserializeObject<ExpandoObject>(json , new ExpandoObjectConverter());
// using dynamic object
var lon = jsonResult.coord.lon;
}
Upvotes: 1