전 세계 날씨에서 웹 사이트의 날씨 정보를 표시하려고합니다. 나는 이것을 만들기 위해 C#으로 VS2012를 사용하고있다.레이블에 결과를 표시하기 위해 XMLDocument 유형을 문자열로 변환하는 방법
XMLDocument 유형 변수 "WP_XMLdoc"아래의 함수에 세계 기상 온라인에서 데이터를 검색 할 수있었습니다.
아래의 코드를 살펴 :public static XmlDocument WeatherAPI(string sLocation)
{
HttpWebRequest WP_Request;
HttpWebResponse WP_Response = null;
XmlDocument WP_XMLdoc = null;
String Value;
string sKey = "xxxxxxxxxxxxxxxxxxxxxxxxx"; //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;
}
}
그래서, 지금은 그냥 "WP_XMLdoc"변수에서 XML 데이터를 먹고 싶어 내 라벨에 temp_c, 풍속, 시간 등과 같은 몇 가지 세부 사항을 보여줍니다. 어떻게하면됩니까? "WP_XMLdoc"에서 휴식을
XML 데이터는 아래와 같습니다 :
<?xml version="1.0" encoding="UTF-8"?>
<data>
<request>
<type>City</type>
<query>London, United Kingdom</query>
</request>
<current_condition>
<observation_time>04:17 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>0</windspeedMiles>
<windspeedKmph>0</windspeedKmph>
<winddirDegree>62</winddirDegree>
<winddir16Point>ENE</winddir16Point>
<precipMM>0.0</precipMM>
<humidity>94</humidity>
<visibility>2</visibility>
<pressure>1010</pressure>
<cloudcover>50</cloudcover>
</current_condition>
<weather>
<date>2014-09-19</date>
<tempMaxC>28</tempMaxC>
<tempMaxF>82</tempMaxF>
<tempMinC>14</tempMinC>
<tempMinF>57</tempMinF>
<windspeedMiles>5</windspeedMiles>
<windspeedKmph>8</windspeedKmph>
<winddirection>SSE</winddirection>
<winddir16Point>SSE</winddir16Point>
<winddirDegree>149</winddirDegree>
<weatherCode>356</weatherCode>
<weatherIconUrl>
<![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0010_heavy_rain_showers.png]]>
</weatherIconUrl>
<weatherDesc>
<![CDATA[Moderate or heavy rain shower]]>
</weatherDesc>
<precipMM>8.3</precipMM>
</weather>
</data>
이 도와주세요! 예를 들어이 같은
: 우리는 단순히 값의 얻을
string
에XElement
캐스팅 할 수이 방법을 사용 :이 기능은
XmlDocument.Load()
유사하다Load()
방법이있다 ? 각 노드에서 XML 또는 값을 덤프하면됩니까? – Samxml 코드를 살펴보십시오./data/current_condition/temp_c,/data/current_condition/windspeedKmph,/data/current_condition/cloudcover 등의 화면에 몇 가지 정보 만 표시하려고합니다. –