使用节点进行5天预测

时间:2013-12-11 16:55:29

标签: c# xml

我已经设置了一个让我的天气好一天的程序,但是我不知道如何获取其他4天的信息,因为在xml文件中所有的日子都有相同的名称。如何访问不同日期的信息?

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

代码:

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 = channel.SelectSingleNode(“item”)。SelectNodes(“yweather:forecast”,manager);             foreach(五天内的XmlNode节点)             {                 var day = node.Attributes [“day”]。Value;                 dayarray [i] =(day);                 //                 var text = node.Attributes [“text”]。Value;                 textarray [i] =(text); 谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

这将为您提供即将到来的日子的集合,然后您可以循环播放它们以获取每天的详细信息:

var upcomingDays = channel.SelectSingleNode("item").SelectNodes("yweather:forecast", manager);

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

我在LINQPad中运行它并且运行正常。

答案 1 :(得分:0)

如果使用像XmlSerializer这样的反序列化器和类,这会容易得多。使用以下类:

[XmlRoot("rss")]
public class RssRoot
{
    [XmlElement("channel")]
    public Channel Channel { get; set; }
}
public class Channel
{
    [XmlElement("item")]
    public Item Item { get; set; }
    // add other properties, if relevant
}
public class Item
{
    [XmlElement("title")]
    public string Title { get; set; }
    [XmlElement("forecast", Namespace="http://xml.weather.yahoo.com/ns/rss/1.0")]
    public List<Forecast> Forecasts { get; set; }
    // add other properties, if relevant
}
public class Forecast
{
    [XmlAttribute("text")]
    public string Text { get; set; }
    // add other attributes, if relevant
}

这将从query

中的网址获取您的数据
string query = "http://weather.yahooapis.com/forecastrss?w=2473224";
using (var reader = XmlReader.Create(query))
{
    var ser = new XmlSerializer(typeof(RssRoot));
    var rss = (RssRoot)ser.Deserialize(reader);
    // use rss
}

如果您想继续使用XmlDocument处理XML,则应使用SelectNodes来获取多个forecast节点,而不是获取单个节点。

答案 2 :(得分:0)

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

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

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