【发布时间】:2019-01-25 20:34:12
【问题描述】:
我目前正在编写一个使用 Visual Studio 和 C# 的 Web 应用程序,而且我对这个编码领域还很陌生。我在用户输入国家/地区开始时遇到问题,代码将从 url XML 读取并以字符串格式显示出来。任何帮助或示例将不胜感激。谢谢!
这是我要阅读的 XML 示例,其中 q=(yourcountry) 是我将在文本框中设置的用户输入
https://samples.openweathermap.org/data/2.5/weather?q=(your country) &mode=xml&appid=b6907d289e10d714a6e88b30761fae22
https://samples.openweathermap.org/data/2.5/weather?q=London&mode=xml&appid=b6907d289e10d714a6e88b30761fae22>
-编辑 我已经尝试过这段代码,但我得到一个错误
我正在尝试从该 URL 中提取温度并输出到标签,但是当我按下提交按钮时,我得到了一个正在加载的错误,我的代码有什么问题吗?
String country = TextBox1.Text;
XmlDocument doc1 = new XmlDocument();
doc1.Load(("http://api.openweathermap.org/data/2.5/weather?q="+country+"&mode=xml&APPID=*CHANGEDAPPID*"));
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/response/current_observation");
foreach (XmlNode node in nodes)
{
string temperaturev = node["temperature.value"].InnerText;
Label1.Text = temperaturev;
【问题讨论】:
-
Read xml from URL的可能重复