• Register
  • Log in
  • Home
  • Contact

Get Yahoo Woeid by City Name



To get the Yahoo Woeid by City name 

using (WebClient wc = new WebClient())
                {
                    results = wc.DownloadString("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22" + CityName + "%22&format=xml");
                    getCityWoeid(results);  //get Woeid from xml
                }

 

private void getCityWoeid(string rawXml)

 

        cityList = new List<yahooWeather>();
            try
            {
                var xml = XDocument.Parse(rawXml);
                string nameSpace = "http://where.yahooapis.com/v1/schema.rng";
                var testData = XName.Get("woeid", nameSpace);
                if (null != testData)
                {
                    List<XElement> forecasts = xml.Descendants(XName.Get("place", nameSpace)).ToList();
                    foreach(var item in forecasts)
                    {
                        yahooWeather weather = new yahooWeather();
                        var Woeid = item.Descendants(XName.Get("woeid", nameSpace)).FirstOrDefault();
                        if(null != Woeid && Woeid.FirstNode != null)
                            weather.Woeid = Woeid.FirstNode.ToString();
                        var City = item.Descendants(XName.Get("name", nameSpace)).FirstOrDefault();
                        if(City != null && City.FirstNode != null)
                            weather.City = City.FirstNode.ToString();                
                        var Country = item.Descendants(XName.Get("country", nameSpace)).FirstOrDefault();
                        if (Country != null && Country.FirstNode != null)
                            weather.Country = Country.FirstNode.ToString();
                        var Province = item.Descendants(XName.Get("admin1", nameSpace)).FirstOrDefault();
                        if(Province != null && Province.FirstNode != null)
                            weather.Province = Province.FirstNode.ToString();
                        var District = item.Descendants(XName.Get("admin2", nameSpace)).FirstOrDefault();
                        if (District != null && District.FirstNode != null)
                            weather.District = District.FirstNode.ToString();
                        cityList.Add(weather);
                    }
                }
                gridView.DataSource = cityList;  //Display list of cities
            }
            catch(Exception ex)
            {
 
            }
}

Yahoo weather Class

public class yahooWeather
{
    public string City { get; set; }
    public string Province { get; set; }
    public string Country { get; set; }
    public string Woeid { get; set; }
    public string District { get; set; }
}

© 2021 - KodeCenter beta