如何在列表框中显示XML值或如何在标签上显示。
下面给出的是我的代码,可以从世界天气在线获取天气详情。代码工作正常,我得到变量“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.
最好使用的是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>
输出:
什么是元素和后裔之间的差异。
要素仅查找那些直接后代的元素,即直系子女。
后裔找到任何级别的儿童,即儿童,孙儿等...
先生,我必须检查千位或千位以上的天气详情。多次使用该页面的用户。那么我如何修改上面的代码呢? – Vipin 2014-09-20 05:03:51
我想查看印度的天气详情,我该如何检查?再次我想添加的位置在我的编码啊?如果是这样,那更复杂一点,它可能会根据我在gridview中的选择而改变。 – Vipin 2014-09-20 05:07:32
我想你只需要将国家代码发送到WeatherAPI方法,如上所述。目前我只是通过伦敦。当然,您可以通过组合框控件根据用户选择传递许多选项。现在就如何使用组合框,我相信你可以找到很多教程。无论有多少用户拨打电话,上述代码都可以工作;只要API允许你这样做。 – codebased 2014-09-20 05:14:36
XML如何看起来像? – codebased 2014-09-20 04:30:30
请在此提供sKey?或提供xml返回值格式 – 2014-09-20 04:32:27