初学者尝试在C#中制作天气预报应用程序,可以';我不明白为什么我的代码会赢';行不通

初学者尝试在C#中制作天气预报应用程序,可以';我不明白为什么我的代码会赢';行不通,c#,api,weather-api,openweathermap,C#,Api,Weather Api,Openweathermap,所以,在学习了一些教程并使用了一些代码之后,我开始编写一些代码,现在我花了好几个小时试图找出为什么它不起作用。我首先制作了一个Windows窗体应用程序,然后制作了这个。起初它可以找到你输入的任何城市的天气,但我在openweathermap上找不到这样的选项,所以我只把它限制在韩国首尔。但是我不明白为什么当我点击按钮时什么都没发生,我想它应该会在文本框中返回预测。。如果有人能帮忙,我将不胜感激 这是我的完整代码: using System; using System.Collections.G

所以,在学习了一些教程并使用了一些代码之后,我开始编写一些代码,现在我花了好几个小时试图找出为什么它不起作用。我首先制作了一个Windows窗体应用程序,然后制作了这个。起初它可以找到你输入的任何城市的天气,但我在openweathermap上找不到这样的选项,所以我只把它限制在韩国首尔。但是我不明白为什么当我点击按钮时什么都没发生,我想它应该会在文本框中返回预测。。如果有人能帮忙,我将不胜感激

这是我的完整代码:

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)
        {

        }
    }
}

您已将此信息烧录在您请求某个城市(Seul)的链接中:

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

您的代码中没有您在此链接中更改的信息。请试试这个:

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

希望会很好:)

您忘记调用API了吗?看一看,您似乎也忘记了在“string.Format”中使用“city”占位符