以下给出的代码是从世界天气在线获取天气详情。代码工作正常,我得到变量" WP_XMLdoc"的天气详细信息。但问题是变量包含xml格式的值。那么如何分别得到每个值以及如何在标签或文本框上显示这些值。
public static XmlDocument WeatherAPI(string sLocation)
{
HttpWebRequest WP_Request;
HttpWebResponse WP_Response = null;
XmlDocument WP_XMLdoc = null;
string sKey = "********************"; //The API key generated by World Weather Online
string sRequestUrl = "http://api.worldweatheronline.com/free/v1/weather.ashx?format=xml&"; //The request URL for XML format
try
{
//Here we are concatenating the parameters
WP_Request = (HttpWebRequest)WebRequest.Create(string.Format(sRequestUrl + "q=" + sLocation + "&key=" + sKey));
WP_Request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4";
//Making the request
WP_Response = (HttpWebResponse)WP_Request.GetResponse();
WP_XMLdoc = new XmlDocument();
//Assigning the response to our XML object
WP_XMLdoc.Load(WP_Response.GetResponseStream());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
WP_Response.Close();
return WP_XMLdoc; // Here we get the five values from the website in xml format. Now I want those xml values from this "WP_XMLdoc" variable to diplay on textbox or labels.
答案 0 :(得分:0)
您可以使用的最好的是XDocument对象,它可以让您更好地控制XmlDocument。
这是我写的控制台应用程序。
您将使用的主要方法是元素(...)和后代(...);
using System;
using System.Linq;
using System.Net;
using System.Xml;
using System.Xml.Linq;
public class Program
{
public static void Main()
{
var result = WeatherAPI("London");
// loop throw all weather instances...
foreach (var w in result.Descendants("weather"))
{
Console.WriteLine("Weather");
Console.WriteLine("=================");
foreach (var e in w.Elements())
{
Console.WriteLine(string.Format("Key {0} - Value {1}", e.Name, e.Value));
}
}
// if you want to select specific element then use this.
var currentCondition = result.Descendants("current_condition").FirstOrDefault();
if (currentCondition != null)
{
Console.WriteLine("Current Condition");
Console.WriteLine("=================");
foreach (var e in currentCondition.Elements())
{
Console.WriteLine(string.Format("Key {0} - Value {1}", e.Name, e.Value));
}
}
Console.ReadLine();
}
public static XDocument WeatherAPI(string sLocation)
{
HttpWebRequest webRequest;
HttpWebResponse webResponse = null;
XDocument xmlResult = null;
var apiKey = "Your key"; //The API key generated by World Weather Online
var apiEndpoint = "http://api.worldweatheronline.com/free/v1/weather.ashx?format=xml&";
try
{
//Here we are concatenating the parameters
webRequest =
(HttpWebRequest) WebRequest.Create(string.Format(apiEndpoint + "q=" + sLocation + "&key=" + apiKey));
webRequest.UserAgent =
@"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4";
//Making the request
webResponse = (HttpWebResponse) webRequest.GetResponse();
xmlResult = XDocument.Load(webResponse.GetResponseStream());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (webResponse != null)
{
webResponse.Close();
}
}
return xmlResult;
// Here we get the five values from the website in xml format. Now I want those xml values from this "WP_XMLdoc" variable to diplay on textbox or labels.
}
}
XML输出
<data>
<request>
<type>City</type>
<query>London, United Kingdom</query>
</request>
<current_condition>
<observation_time>04:53 AM</observation_time>
<temp_C>17</temp_C>
<temp_F>63</temp_F>
<weatherCode>143</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0006_mist.png]]></weatherIconUrl>
<weatherDesc><![CDATA[Mist]]></weatherDesc>
<windspeedMiles>6</windspeedMiles>
<windspeedKmph>9</windspeedKmph>
<winddirDegree>20</winddirDegree>
<winddir16Point>NNE</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>94</humidity>
<visibility>1</visibility>
<pressure>1014</pressure>
<cloudcover>75</cloudcover>
</current_condition>
<weather>
<date>2014-09-20</date>
<tempMaxC>22</tempMaxC>
<tempMaxF>71</tempMaxF>
<tempMinC>10</tempMinC>
<tempMinF>50</tempMinF>
<windspeedMiles>8</windspeedMiles>
<windspeedKmph>13</windspeedKmph>
<winddirection>N</winddirection>
<winddir16Point>N</winddir16Point>
<winddirDegree>350</winddirDegree>
<weatherCode>119</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png]]></weatherIconUrl>
<weatherDesc><![CDATA[Cloudy ]]></weatherDesc>
<precipMM>0.6</precipMM>
</weather>
</data>
输出:
元素与后代之间有什么区别。
元素只查找那些直接后代的元素,即直接子元素。
后代发现任何级别的儿童,即儿童,孙子等......