Search code examples
c#xmlweather-apiyahoo-weather-api

How to Upgrade to the new Yahoo weather API?


For some time now i have been using the Yahoo Weather Api to get current day temperature and forecasts for statistics in a .Net application in C#. Apparently Yahoo updated their api and the application fails to get the data.

I am using an xml document like this to get the data

    XmlDocument doc = new XmlDocument();
    doc.Load("http://xml.yahooapis.com/forecastrss?w=" + WOEID + "&u=c");
    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
    ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");

    XmlNode nod = doc.SelectSingleNode("/rss/channel/link", ns);
    link = nod.InnerText;
    ....more nodes selected....

and like this i get the xml nodes and values to store them in the database.

What changes do I have to make so that the application will work with the new the api?


Solution

  • First of all we need to change the url that we are asking for the forecast from

     doc.Load("http://xml.yahooapis.com/forecastrss?w=" + WOEID + "&u=c");
    

    to

          query="select%20*%20from%20weather.forecast%20where%20woeid%20%3D%20"+ WOEID 
               + "%20and%20u=%27c%27";
          doc.Load("https://query.yahooapis.com/v1/public/yql?q="+query+"&format=xml
                     &env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
    

    then we need to change the nodes path because the new xml document is slightly different from the old one.

    change from

    XmlNode nod = doc.SelectSingleNode("/rss/channel/link", ns);
    

    to

    XmlNode nod = doc.SelectSingleNode("/query/results/channel/link", ns);
    

    and everything should be working fine.