为什么我不能使用XDocument.Descendants()?

时间:2015-09-09 10:43:32

标签: .net visual-studio c#-4.0 linq-to-xml

大家好,我对XDocument.Descendants()有疑问。我有xml文档https://export.yandex.ru/weather-ng/forecasts/33345.xml,但我无法阅读。 这是我的代码:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var weatherInfo = Function("https://export.yandex.ru/weather-ng/forecasts/33345.xml");
            foreach (var item in weatherInfo)
            {
                Console.WriteLine(item.ToString());
            }
        }
        public static IEnumerable<Weather> Function(string url)
        {
            var document = XDocument.Load(url);
            var weatherInfo = from e in document.Descendants("fact")
                              select new Weather
                              {
                                  Date = e.Element("observation_time").Value,
                                  WeatherType = e.Element("weather_type").Value,
                                  WindDirection = e.Element("wind_direction").Value,
                                  WindSpeed = Convert.ToDouble(e.Element("wind_speed").Value),
                                  Humidity = Convert.ToDouble(e.Element("humidity").Value),
                                  PressureUnits = Convert.ToInt32(e.Element("pressure_units").Value),
                                  Season = e.Element("season").Value
                              };
            return weatherInfo;
        }
    }
    public class Weather
    {
        public string Date { get; set; }
        public string WeatherType { get; set; }
        public string WindDirection { get; set; }
        public double WindSpeed { get; set; }
        public double Humidity { get; set; }
        public int PressureUnits { get; set; }
        public string Season { get; set; }
    }
}

但我没有看到标签&#34;事实&#34;。你能告诉我,我的代码有什么问题吗?谢谢你。

0 个答案:

没有答案