C#从谷歌';香港天气预报

C#从谷歌';香港天气预报,c#,xml,parsing,weather,C#,Xml,Parsing,Weather,我一直在使用这段代码尝试从google weather API获取数据,但我从未接近提取我想要的数据 我的目标是看: <forecast_information> **<city data="london uk"/>** <postal_code data="london uk"/> <latitude_e6 data=""/> <longitude_e6 data=""/> <forecast_date data="2011-1

我一直在使用这段代码尝试从google weather API获取数据,但我从未接近提取我想要的数据

我的目标是看:

<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>
//注意,当前代码设置为显示所有子节点


也许有人能解释一下这件事?

也许你应该使用
节点。选择SingleNode(“城市”).Attributes[“data”]。Value
而不是
节点。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 + " ");
    }
}
换成

history.AppendText(Environment.NewLine + "City : " + node.GetAttribute("data"));

什么是“子节点的文本”?数据属性的值?只是为了补充一点,我做了一个完整的示例,但是使用Linq to XML,它更容易处理。这只是返回一个空行。。这就是我感到很沮丧的地方。我写的每一个代码变体在纸上看起来都很好,但一旦我编译并启动命令,它就什么也没有了。您的
节点
是单个元素
预测信息
。当然,它没有数据属性。
history.AppendText(Environment.NewLine + "City : " + node.GetAttribute("data"));
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);