我目前正在编写一个使用Visual Studio和C#的Web应用程序,并且在此编码领域中我还很陌生。从用户输入国家/地区开始,我会遇到麻烦,代码将从URL XML中读取并以字符串格式显示出来。任何帮助或示例将不胜感激。谢谢!
这是我要阅读的XML的示例,其中q =(您的国家/地区)是我将在文本框中设置的用户输入
https://samples.openweathermap.org/data/2.5/weather?q=(your country) &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;
答案 0 :(得分:0)
使用XML linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication98
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
Weather weather = new Weather();
XDocument doc = XDocument.Load(FILENAME);
XElement current = doc.Descendants("current").FirstOrDefault();
XElement city = current.Element("city");
weather.cityID = (string)city.Attribute("id");
weather.cityName = (string)city.Attribute("name");
weather.lat = (decimal)city.Element("coord").Attribute("lat");
weather.lon = (decimal)city.Element("coord").Attribute("lon");
weather.country = (string)city.Element("country");
weather.surnrise = (DateTime)city.Element("sun").Attribute("rise");
weather.sunset = (DateTime)city.Element("sun").Attribute("set");
XElement temperature = current.Element("temperature");
weather.temperature = (decimal)temperature.Attribute("value");
weather.mintemperature = (decimal)temperature.Attribute("min");
weather.mintemperature = (decimal)temperature.Attribute("max");
weather.temperatureUnit = (string)temperature.Attribute("unit");
XElement humidity = current.Element("humidity");
weather.humidity = (decimal)humidity.Attribute("value");
weather.humidityUnit = (string)humidity.Attribute("unit");
XElement pressure = current.Element("pressure");
weather.pressure = (decimal)pressure.Attribute("value");
weather.pressureUnit = (string)pressure.Attribute("unit");
XElement wind = current.Element("wind");
weather.windSpeed = (decimal)wind.Element("speed").Attribute("value");
weather.windType = (string)wind.Element("speed").Attribute("name");
weather.windDirectionDegrees = (int)wind.Element("direction").Attribute("value");
weather.windDirectionString = (string)wind.Element("direction").Attribute("name");
XElement clouds = current.Element("clouds");
weather.cloudPercentage = (int)clouds.Attribute("value");
weather.cloudType = (string)clouds.Attribute("name");
XElement visibility = current.Element("visibility");
weather.visibility = (int)visibility.Attribute("value");
XElement precipitation = current.Element("precipitation");
weather.precipitation = (string)precipitation.Attribute("mode");
XElement xLastUpdate = current.Element("lastupdate");
weather.lastUpdate = (DateTime)xLastUpdate.Attribute("value");
}
}
public class Weather
{
public string cityName { get; set; }
public string cityID { get; set; }
public decimal lon { get; set; }
public decimal lat { get; set; }
public string country { get; set; }
public DateTime surnrise { get; set; }
public DateTime sunset { get; set; }
public decimal temperature { get; set; }
public decimal mintemperature { get; set; }
public decimal maxtemperature { get; set; }
public string temperatureUnit { get; set; }
public decimal humidity { get; set; }
public string humidityUnit { get; set; }
public decimal pressure { get; set; }
public string pressureUnit { get; set; }
public decimal windSpeed { get; set; }
public string windType { get; set; }
public int windDirectionDegrees { get; set; }
public string windDirectionString { get; set; }
public int cloudPercentage { get; set; }
public string cloudType { get; set;}
public int visibility { get; set; }
public string precipitation { get; set; }
public DateTime lastUpdate { get; set; }
}
}