0

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 {}
    }
  • Welcome to StackOverflow! Have you tried using Request.QueryString? – OutstandingBill Oct 18 '18 at 00:13
  • Also, when posting, it's worth considering how general you can make your question. It looks like you have already figured out the Yql and Yahoo-api side of things, and that the question is really about how to get information from the query string. Once you've boiled the question down to its bare essentials, try searching again. I'm sure you'll come up with heaps of resources that way. – OutstandingBill Oct 18 '18 at 00:18
  • yea bill i tried 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 city="WoeidTxtBox.Text")&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys") but its not working – user10521160 Oct 18 '18 at 00:41

3 Answers3

0

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");


       }


    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Hey jdweng i want to get the weather information based on the woeid the user enters in the textbox so it can give the weather state based on the user location because what if the user is not from los angeles – user10521160 Oct 18 '18 at 00:37
0

Add the line

var woeid = Request.QueryString["woeid"];

then include it in your existing query like this:

String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid = '{0}'&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", woeid);
OutstandingBill
  • 2,614
  • 26
  • 38
0

I like solution below which handles multiple cities with the same name

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Threading;

namespace WindowsFormsApplication23
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] cities = { "Los Angeles", "New York", "Salem", "Portland", "Washington" };
            DataTable dt = GetWeather(cities);
            dataGridView1.DataSource = dt;
        }

        DataTable GetWeather(string[] cities)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("City", typeof(string));
            dt.Columns.Add("Region", typeof(string));
            dt.Columns.Add("Humidity", typeof(int));
            dt.Columns.Add("Wind Speed", typeof(int));
            dt.Columns.Add("Temperature", typeof(int));
            dt.Columns.Add("Condition", typeof(string));
            dt.Columns.Add("High Temperature", typeof(int));
            dt.Columns.Add("Low Temperature", typeof(int));

            foreach (string inputCity in cities)
            {
                String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(100) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", inputCity);
                XDocument wData = XDocument.Load(query);
                XNamespace ns = wData.Root.GetDefaultNamespace();
                //System.Threading.Thread.Sleep(10000);
                foreach (XElement channel in wData.Descendants().Where(x => x.Name.LocalName == "channel"))
                {
                    string city = "";
                    string region = "";
                    int? humidity = null;
                    int? speed = null;
                    int? temp = null;
                    string tfCond = "";
                    int? high = null;
                    int? low = null;

                     XNamespace yWeatherNs = channel.Elements().First().GetNamespaceOfPrefix("yweather");

                    XElement xLocation = channel.Element(yWeatherNs + "location");
                    if (xLocation == null)
                    {
                        continue;
                    }
                    else
                    {
                        city = (string)xLocation.Attribute("city");
                        region = (string)xLocation.Attribute("region");
                    }

                    XElement xAtmosphere = channel.Element(yWeatherNs + "atmosphere");
                    if (xAtmosphere != null)
                    {
                        humidity = (int)xAtmosphere.Attribute("humidity");
                    }

                    XElement xWind = channel.Element(yWeatherNs + "wind");
                    if (xWind != null)
                    {
                        speed = (int)xWind.Attribute("speed");
                    }

                    XElement item = channel.Element("item");
                    if (item != null)
                    {

                        XElement xCondition = item.Element(yWeatherNs + "condition");
                        if (xCondition != null)
                        {
                            temp = (int)xCondition.Attribute("temp");
                        }


                        List<XElement> xForecast = item.Elements(yWeatherNs + "forecast").ToList(); ;
                        if (xForecast != null)
                        {
                            tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
                            high = (int)xForecast.FirstOrDefault().Attribute("high");
                            low = (int)xForecast.FirstOrDefault().Attribute("low");
                        }
                    }

                    dt.Rows.Add(new object[] { city, region, humidity, speed, temp, tfCond, high, low });
                }                
            }
            dt = dt.AsEnumerable().OrderBy(x => x.Field<string>("City")).ThenBy(x => x.Field<string>("Region")).CopyToDataTable();
            return dt;
        }
    }
}

enter image description here

jdweng
  • 33,250
  • 2
  • 15
  • 20