更改全球天气服务的显示以进行休息应用

时间:2014-05-20 21:10:06

标签: c# asp.net web-services rest

我正在使用webservicex网站上的全球气象服务。

我有一个表单,用户在城市中输入,应用程序返回该城市的天气数据。

问题是所显示的内容非常有趣,我不知道如何更改它。

这是我的代码

 string url = String.Format("http://www.webservicex.com/globalweather.asmx/GetWeather?CountryName=Slovenia&CityName=" + HttpUtility.UrlEncode(city.Text));

        WebClient client = new WebClient();
        display.Text = client.DownloadString(url);

这是结果,当它在城市中输入

Vreme: <?xml version="1.0" encoding="utf-16"?> <CurrentWeather> <Location>Maribor /     Slivnica, Slovenia (LJMB) 46-29N 015-41E 265M</Location> <Time>May 20, 2014 - 04:30   PM EDT /   2014.05.20 2030 UTC</Time> <Wind> from the SSW (200 degrees) at 6 MPH (5 KT) (direction variable):0</Wind> <Visibility> greater than 7 mile(s):0</Visibility> <Temperature> 62 F (17 C)</Temperature> <DewPoint> 48 F (9 C)</DewPoint> <RelativeHumidity> 59%</RelativeHumidity> <Pressure> 30.03 in. Hg (1017 hPa)</Pressure> <Status>Success</Status> </CurrentWeather>

你能帮我把这个显示变成更正常的东西吗?

任何帮助都将不胜感激。谢谢!

显示的数据应如下所示。

地点:斯洛文尼亚马里博尔/斯利文尼察(LJMB)46-29N 015-41E 265M

温度:62 F(17 C)

2 个答案:

答案 0 :(得分:0)

您可以使用XmlReader对象在C#中解析XML。

http://msdn.microsoft.com/en-us/library/cc189056.aspx

答案 1 :(得分:0)

我发现XDocument是处理XML的最简单方法。

通过获取根,然后专门查找您的元素,这些值并不难获得。

  var doc = XDocument.Parse(xmlstring);  //use .Load if you are pulling an xml file.
  var location = doc.Root.Element("Location").Value;
  var temp = doc.Root.Element("Temperature").Value;
  Console.Write("Location: {0} {1}Temperature: {2}", 
                 location, Environment.NewLine, temp);

DotNet Fiddle