2014-09-20 90 views
0

下面給出的是我的代碼,可以從世界天氣在線獲取天氣詳情。代碼工作正常,我得到變量「WP_XMLdoc」的天氣細節。但問題是變量包含的值是xml格式。因此,如何分別獲取每個值以及如何在標籤或文本框中顯示這些值。如何在列表框中顯示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

XML如何看起來像? – codebased 2014-09-20 04:30:30

+0

請在此提供sKey?或提供xml返回值格式 – 2014-09-20 04:32:27

回答

0

最好使用的是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> 

輸出:

enter image description here

什麼是元素和後裔之間的差異。

要素僅查找那些直接後代的元素,即直系子女。

後裔找到任何級別的兒童,即兒童,孫兒等...

+0

先生,我必須檢查千位或千位以上的天氣詳情。多次使用該頁面的用戶。那麼我如何修改上面的代碼呢? – Vipin 2014-09-20 05:03:51

+0

我想查看印度的天氣詳情,我該如何檢查?再次我想添加的位置在我的編碼啊?如果是這樣,那更復雜一點,它可能會根據我在gridview中的選擇而改變。 – Vipin 2014-09-20 05:07:32

+0

我想你只需要將國家代碼發送到WeatherAPI方法,如上所述。目前我只是通過倫敦。當然,您可以通過組合框控件根據用戶選擇傳遞許多選項。現在就如何使用組合框,我相信你可以找到很多教程。無論有多少用戶撥打電話,上述代碼都可以工作;只要API允許你這樣做。 – codebased 2014-09-20 05:14:36

相關問題