c# - 如何使用 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;
}
解决方案
推荐阅读
- javascript - 如何更改索引表达式的类型?即:c[k]
- imap - 如果邮箱名称包含非英文符号,则返回“BAD could not parse command”
- css - CSS 拒绝加载 Springboot / Spring Security / Thymeleaf
- c - 为什么逻辑语句 !0x00 在 C 中返回 0x01?
- node.js - Socket.io 使用 Reactjs 和 Expressjs 在 GET 请求中发送数据
- usb - DFU模式下的STM32F04仅在电池供电时识别(Win10)
- .htaccess - Symfony 4 + AWS Cloudfront 作为 PageCache + AWS ELB 导致首页上无休止的重定向
- c++ - 当问题的性质完全不同时,为什么要使用 C2259?
- python - 如何查看任务是否完成?
- java - 如何制作录音机