首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用节点的5天预测

使用节点的5天预测
EN

Stack Overflow用户
提问于 2013-12-11 16:55:28
回答 2查看 841关注 0票数 0

我已经建立了一个程序来获取1天的晴天天气,但是我不知道如何获得其他4天的信息,因为在xml文件中,天都有相同的名称。我如何访问不同日子的信息?

http://weather.yahooapis.com/forecastrss?w=2473224我正在使用的Xml

代码:

代码语言:javascript
复制
string query = String.Format("http://weather.yahooapis.com/forecastrss?w=2473224");
XmlDocument wData = new XmlDocument();
wData.Load(query);

XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");

XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager);

string temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;

string condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;

string humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value;

string WindSpeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;

string town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;

string tfcond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["text"].Value;

string tfhigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value;

string tflow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["low"].Value;

我把这些值赋值给这样的数组,并使用这样的代码来解决这个问题。

var fiveDays =fiveDays管理器);foreach (fiveDays中的XmlNode节点){ var day = node.Attributes"day".Value;dayarrayi = (day);// var text = node.Attributes"text".Value;textarrayi = (text);感谢您的帮助!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-11 17:11:19

我相信您可以使用XmlNode.SelectNodes方法而不是SelectSingleNode。

这将返回一个列表,然后您可以迭代该列表并将值复制到需要它们的位置。

代码语言:javascript
复制
var fiveDays = channel.SelectSingleNode("item").SelectNodes("yweather:forecast", manager);
foreach (XmlNode node in fiveDays)
{
    var text = node.Attributes["text"].Value;
    var high = node.Attributes["high"].Value;
    var low = node.Attributes["low"].Value;
}
票数 0
EN

Stack Overflow用户

发布于 2013-12-11 17:08:46

这将为您提供即将到来的几天的集合,然后您可以遍历它们以获取每天的详细信息:

代码语言:javascript
复制
var upcomingDays = channel.SelectSingleNode("item").SelectNodes("yweather:forecast", manager);

foreach(XmlNode d in upcomingDays)
{
    //d.Attributes["day"].Value;
}

我在LINQPad上运行了这个,它运行得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20525188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档