C#从Google的天气API中提取XML数据 [英] C# Pull XML data from google's weather API

查看:98
本文介绍了C#从Google的天气API中提取XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用此代码来尝试从谷歌天气API中获取数据,但我什至无法接近想要的东西.

I've been using this code to try and get data from the google weather API, but I never get even close to pulling out what i want.

我的目标是看:

<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>

并且仅返回子节点的文本.

And only return the text of the child nodes.

因此结果将是:

城市:英国伦敦 温度:20度 湿度:68% 风速:22mph

City: London UK Temp: 20c Humidity: 68% Wind: 22mph

目前,我正在尝试使用此功能,但无济于事...

Currently I am trying to use this, but got nowhere...

 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);
 }

//注意,当前代码已设置为显示所有子节点

//NOTE, currently code is set to display ALL child nodes

也许有人可以阐明这件事吗?

Perhaps someone can shed some light on the matter?

推荐答案

也许您应该使用node.SelectSingleNode("city").Attributes["data"].Value而不是node.InnerText

Maybe you should use node.SelectSingleNode("city").Attributes["data"].Value instead of 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 + " ");
    }
}

这篇关于C#从Google的天气API中提取XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆