c# - 尝试用 C# 制作天气程序,但除了 API 中的国家/地区代码外,它不会返回任何内容

标签 c# xml xml-parsing weather-api 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)
        {


        }

        private void button1_Click(object sender, EventArgs e)
        {

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

            XDocument doc = XDocument.Load(uri);


            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("wind.speed.unit").FirstOrDefault();
            string humidity = (string)doc.Descendants("humidity.value").FirstOrDefault();

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


            txtmaxtemp.Text = maxTemp;
            txtmintemp.Text = minTemp;

            txtwindm.Text = maxWindm;
            txthumidity.Text = humidity;

            txtcountry.Text = country;

        }
    }
}

最佳答案

给定 uri 的 XML 实际上如下所示:

<current>
  <city id="1835848" name="Seoul">
    <coord lon="126.98" lat="37.57"/>
    <country>KR</country>
    <timezone>32400</timezone>
    <sun rise="2019-12-06T22:33:04" set="2019-12-07T08:13:43"/>
  </city>
  <temperature value="270.15" min="266.15" max="274.15" unit="kelvin"/>
  <humidity value="86" unit="%"/>
  <pressure value="1029" unit="hPa"/>
  <wind>
    <speed value="1.25" unit="m/s" name="Calm"/>
    <gusts/>
    <direction value="302" code="WNW" name="West-northwest"/>
  </wind>
  <clouds value="90" name="overcast clouds"/>
  <visibility value="10000"/>
  <precipitation mode="no"/>
  <weather number="804" value="overcast clouds" icon="04n"/>
  <lastupdate value="2019-12-07T12:41:00"/>
</current>

在查看country-节点时,您可以看到它是一个 xml 节点。相反,min-和max-温度是temperence节点的属性

您可以像这样访问属性:

string maxTemp = (string)doc.Descendants("temperature").FirstOrDefault().Attribute("max").Value;

请注意,这可行,但很容易出现 NullReferenceException,因为我盲目地使用 FirstOrDefault() ;-)

关于c# - 尝试用 C# 制作天气程序,但除了 API 中的国家/地区代码外,它不会返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59226087/

相关文章:

android - 如何在我的半透明 EditText 背景后面放置一个不透明的白色矩形?

javascript - 使用 xml 文件初始化 javascript 程序

python - 在 Python 的 ElementTree 中提取标签后的文本

c# - InvalidCastException : Unable to cast object of type 'System.DBNull' to type 'System.Nullable` 1[System. Int32]'

c# - 如何根据键值获取在文本文件中输入的记录

c# - .NET 4.0 框架中是否已经存在以下泛型方法?

java - Jsoup : SelectorParseException when colon in xml tag

c# - ASP.net 防止在编辑模式下单击 gridview 行

javascript - 如何在CasperJS中选择带有命名空间的标签?

xml - XSLT - 如何按顺序读取所有子节点,具有相同和不同的名称标签