首页 > 解决方案 > 从 URL 读取 XML 数据

问题描述

我目前正在编写一个使用 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 >

-edit 我试过这段代码,但我得到一个错误

我正在尝试从此 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;

标签: c#xml

解决方案


使用 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; }

    }




}

推荐阅读