我想使用Yahoo weather API。但以前的API已经变得未经授权。如何在我的C#代码中使用雅虎天气RSS?

时间:2016-04-29 17:48:48

标签: c# xml api rss

我想使用Yahoo weather API。但以前的API已经变得未经授权。如何在我的C#代码中使用雅虎天气RSS? 我一直试图用这个:

https://query.yahooapis.com/v1/public/yql?q=select%20%20from%20weather.forecast%20where%20woeid%3D1915035%20and%20u%3D%27c%27&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

但它似乎会将Null返回到我的XmlNode

1 个答案:

答案 0 :(得分:0)

private void GetWeather()
{
    string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D1915035%20and%20u%3D%27c%27&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
    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");//exception here
    XmlNodeList nodes = wData.SelectNodes("rss/channel/item/yweather:forecast", manager);

    Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;
    Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
    Humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value;
    WindSpeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;
    Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;
    TPCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["text"].Value;
    TPHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value;
    TPLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["low"].Value;        
}