最好使用的是XDocument對象,它可以更好地控制XmlDocument。
這是我寫過的控制檯應用程序。
您將使用的主要方法是Element(...)和Descendents(...);
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>
輸出:
什麼是元素和後裔之間的差異。
要素僅查找那些直接後代的元素,即直系子女。
後裔找到任何級別的兒童,即兒童,孫兒等...
XML如何看起來像? – codebased 2014-09-20 04:30:30
請在此提供sKey?或提供xml返回值格式 – 2014-09-20 04:32:27