首页 > 解决方案 > 如何使用 c# Web 应用程序 Web 表单从 api 输出数据?

问题描述

我是初学者,我想制作调用 API 的 c#.net web 表单。当我做我的代码时没有错误,但数据没有显示,它只是一个空白。有人可以向我解释为什么吗?我正在使用 xml 类型的 API。提前致谢 !这是我的代码:

这是 xml 文件中的示例之一,但它具有打开和关闭标记:

`

<current>
<city id="2756136" name="Emmen">
<coord lon="6.91" lat="52.78"/>
<country>NL</country>
<timezone>7200</timezone>
<sun rise="2020-06-08T03:09:19" set="2020-06-08T19:53:53"/>
</city>
<temperature value="286.14" min="285.15" max="286.48" unit="kelvin"/>
<feels_like value="284.26" unit="kelvin"/>
<humidity value="87" unit="%"/>
<pressure value="1012" unit="hPa"/>
<wind>
<speed value="3.1" unit="m/s" name="Light breeze"/>
<gusts/>
<direction value="220" code="SW" name="Southwest"/>
</wind>
<clouds value="75" name="broken clouds"/>
<visibility value="10000"/>
<precipitation mode="no"/>
<weather number="803" value="broken clouds" icon="04d"/>
<lastupdate value="2020-06-08T07:08:14"/>
</current>

`

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

      city = TypeCity.Text;
      string url = string.Format("http://api...", city);

      XDocument Document = XDocument.Load(url);

      WebClient webClient = new WebClient();
      string Temperature = (string)Document.Descendants("temperature.value").FirstOrDefault();

      string Humidity = (string)Document.Descendants("humidity.value").FirstOrDefault();

      string WindSpeed = (string)Document.Descendants("timezone").FirstOrDefault();

      string Country = (string)Document.Descendants("country").FirstOrDefault();


      temp.Text = Temperature;

      txtHumidty.Text = Humidity;

      txtWind.Text = WindSpeed;

      cityDisplay.Text = city;
 }

标签: c#asp.netxmlapiwebforms

解决方案


推荐阅读