0

So after following some tutorials and using https://openweathermap.org/current, I got some code going and now I've been spending hours trying to figure out why it won't work. I started by making a Windows Forms app and made this interface. At first it was going to be able to find the weather of any city you typed in, but I can't find such an option on openweathermap so I only restricted it to Seoul, Korea. However I don't understand why nothing happens when I click the button, I thought it should bring back the forecast in the text boxes.. If anyone could help it would be really appreciated.

This is my full code:

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


namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string city;

            city = txtcity.Text;

            string uri = string.Format("http://api.openweathermap.org/data/2.5/weather?q=Seoul&mode=xml&appid=78dff84492be32f8b4f77692904607a1", city);

            XDocument doc = XDocument.Load(uri);

            string iconUri = (string)doc.Descendants("icon").FirstOrDefault();

            WebClient client = new WebClient();


            string maxtemp = (string)doc.Descendants("temperature.max").FirstOrDefault();
            string mintemp = (string)doc.Descendants("temperature.min").FirstOrDefault();

            string maxwindm = (string)doc.Descendants("maxwind_mph").FirstOrDefault();
            string maxwindk = (string)doc.Descendants("maxwind_kph").FirstOrDefault();
            string humidity = (string)doc.Descendants("avghumidity").FirstOrDefault();

            string country = (string)doc.Descendants("country").FirstOrDefault();

            string cloud = (string)doc.Descendants("text").FirstOrDefault();


            txtmaxtemp.Text = maxtemp;
            txtmintemp.Text = mintemp;

            txtwindm.Text = maxwindm;
            txtwindk.Text = maxwindk;

            txthumidity.Text = humidity;

            label7.Text = cloud;
            txtcountry.Text = country;

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
chankim
  • 23
  • 1
  • 3
  • Did you forget to call the API? Have a look at https://stackoverflow.com/questions/9620278/how-do-i-make-calls-to-a-rest-api-using-c – Nabi Sobhi Dec 07 '19 at 07:29
  • 1
    It seems you also forget to use a placeholder for the 'city' inside 'string.Format' – Nabi Sobhi Dec 07 '19 at 07:33

1 Answers1

0

You have burned this info in your link that you are asking for a certain city (Seul):

"http://api.openweathermap.org/data/2.5/weather?q***=Seoul***&mode=xml&appid=78dff84492be32f8b4f77692904607a1"

There is no info in your code that you changed it in this link. Please try this:

String.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&mode=xml&appid=78dff84492be32f8b4f77692904607a1",city);

Hope will fine :)

MORFEE89
  • 137
  • 4