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

How to use yahoo Yql query to gather weather information using woeid number


I have a textbox where i want the user to input their woeid number but i am not sure how to add it to the query string, in the code below i can get the weather for Los Angeles, but what i want now is to get it by using the woeid number the user provides.

        try
        {

            String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='Los Angeles')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
            var wData = new XmlDocument();
            wData.Load(query);

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

            XmlNode channel = wData.SelectSingleNode("query").SelectSingleNode("results").SelectSingleNode("channel");
            XmlNodeList nodes = wData.SelectNodes("query/results/channel");

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

            MainForm.Town = channel.SelectSingleNode("yweather:location", man).Attributes["city"].Value;

            MainForm.Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["temp"].Value;

            MainForm.Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["text"].Value;

            MainForm.Humidity = channel.SelectSingleNode("yweather:atmosphere", man).Attributes["humidity"].Value;

            MainForm.TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["text"].Value;

            MainForm.TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["high"].Value;

            MainForm.TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["low"].Value;              
        }
        catch {}
    }

Solution

  • Try xml linq :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
    
            static void Main(string[] args)
            {            
                string city = "Los Angeles";
                string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", city);
    
                XDocument wData = XDocument.Load(query);
                XNamespace ns = wData.Root.GetDefaultNamespace();
    
                XElement xWind = wData.Descendants().Where(x => x.Name.LocalName == "wind").FirstOrDefault();
                int speed = (int)xWind.Attribute("speed");
                XElement xLocation = wData.Descendants().Where(x => x.Name.LocalName == "location").FirstOrDefault();
                string town = (string)xLocation.Attribute("city");
    
                XElement xCondition = wData.Descendants().Where(x => x.Name.LocalName == "condition").FirstOrDefault();
                int temp = (int)xCondition.Attribute("temp");
    
                XElement xAtmosphere = wData.Descendants().Where(x => x.Name.LocalName == "atmosphere").FirstOrDefault();
                int humidity = (int)xAtmosphere.Attribute("humidity");
    
                List<XElement> xForecast = wData.Descendants().Where(x => x.Name.LocalName == "forecast").ToList(); ;
                string tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
                int high = (int)xForecast.FirstOrDefault().Attribute("high");
                int low = (int)xForecast.FirstOrDefault().Attribute("low");
    
    
           }
    
    
        }
    
    
    }