1

私はこのコードを使用してGoogleWeatherAPIからデータを取得しようとしていますが、必要なものを引き出すことすらできません。

私の目標は以下を見ることです:

<forecast_information>
**<city data="london uk"/>**
<postal_code data="london uk"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2011-10-09"/>
<current_date_time data="2011-10-09 12:50:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Partly Cloudy"/>
<temp_f data="68"/>
**<temp_c data="20"/>**
**<humidity data="Humidity: 68%"/>**
<icon data="/ig/images/weather/partly_cloudy.gif"/>
**<wind_condition data="Wind: W at 22 mph"/>**
</current_conditions>

そして、子ノードのテキストのみを返します。

したがって、結果は次のようになります。

市:ロンドン英国気温:20c湿度:68%風:22mph

現在、これを使おうとしていますが、どこにも行きません...

 XmlDocument doc = new XmlDocument();
 XmlNodeList _list = null;
 doc.Load("http://www.google.com/ig/api?weather=london+uk");
 _list = doc.GetElementsByTagName("forecast_information/");
 foreach (XmlNode node in _list)
 {
 history.AppendText(Environment.NewLine + "City : " + node.InnerText);
 }

//注、現在、コードはすべての子ノードを表示するように設定されています

おそらく誰かがその問題に光を当てることができますか?

4

3 に答える 3

2

多分あなたはnode.SelectSingleNode("city").Attributes["data"].Value代わりに使うべきですnode.InnerText

-編集-これは私のために働きます

XmlDocument doc = new XmlDocument();
doc.Load("http://www.google.com/ig/api?weather=london+uk");
var list = doc.GetElementsByTagName("forecast_information");
foreach (XmlNode node in list)
{
    Console.WriteLine("City : " + node.SelectSingleNode("city").Attributes["data"].Value);
}

list = doc.GetElementsByTagName("current_conditions");
foreach (XmlNode node in list)
{
    foreach (XmlNode childnode in node.ChildNodes)
    {
        Console.Write(childnode.Attributes["data"].Value + " ");
    }
}
于 2011-10-09T15:31:42.603 に答える
0

これをに変更します

history.AppendText(Environment.NewLine + "City : " + node.GetAttribute("data"));
于 2011-10-09T15:35:01.743 に答える
0
using System.Xml.Linq;
using System.Xml.XPath;

XElement doc = XElement.Load("http://www.google.com/ig/api?weather=london+uk");
string theCity = doc.XPathSelectElement(@"weather/forecast_information/city").Attribute("data").Value;
string theTemp = doc.XPathSelectElement(@"weather/current_conditions/temp_c").Attribute("data").Value;
string theHumid = doc.XPathSelectElement(@"weather/current_conditions/humidity").Attribute("data").Value;
string theWind = doc.XPathSelectElement(@"weather/current_conditions/wind_condition").Attribute("data").Value;

string resultString = String.Format("City : {0} Temp : {1}c {2} {3}", theCity, theTemp, theHumid, theWind);
于 2011-10-09T16:00:05.773 に答える