Answers for "wetter in c# einbinden"

C#
0

wetter in c# einbinden

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.IO;
 
namespace WetterChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Geben Sie den Stadtnamen und das Länderkürzel ein (bsp. \"Berlin, de\")");
            string eingabe = Console.ReadLine();
            string[] parameter = eingabe.Split(',');
            string url = @"http://api.openweathermap.org/data/2.5/forecast?q=" + parameter[0].Trim() + "," + parameter[1].Trim() + "&mode=xml";
 
            try
            {
                XElement root = XElement.Load(url, LoadOptions.PreserveWhitespace);
 
                if (root != null)
                {
                    XElement location = root.Element("location");
                    XElement stadt = location.Element("name");
                    XElement land = location.Element("country");
 
                    XAttribute sonnenaufgang = root.Element("sun").Attribute("rise");
                    XAttribute sonnenuntergang = root.Element("sun").Attribute("set");
                    DateTime datum = DateTime.Parse((string)sonnenaufgang);
                    DateTime datum2 = DateTime.Parse((string)sonnenuntergang);
 
                    XElement vorhersage = root.Element("forecast");
                    XElement aktuell = vorhersage.FirstNode.NextNode as XElement;
 
                    XElement wind = vorhersage.Element("time").Element("windDirection");
                    XElement temperatur = vorhersage.Element("time").Element("temperature");
                    XElement luftdruck = vorhersage.Element("time").Element("pressure");
                    XElement luftfeuchtigkeit = vorhersage.Element("time").Element("humidity");
                    XElement wolken = vorhersage.Element("time").Element("clouds");
 
 
                    Console.WriteLine("\nStadt: " + (string)stadt + "\nLand: " + (string)land + "\n\nDatum: " + datum.ToShortDateString() + "\nSonnenaufgang: " + datum.AddHours(1).ToShortTimeString() +
                                      " Uhr \nSonnenuntergang: " + datum2.AddHours(1).ToShortTimeString() + " Uhr \nWindrichtung: " + (string)wind.Attribute("name") + "\nTemperatur: " +
                                      (string)temperatur.Attribute("value") + "° " + (string)temperatur.Attribute("unit") + "\nLuftdruck: " + (string)luftdruck.Attribute("value") + " " +
                                      (string)luftdruck.Attribute("unit") + "\nLuftfeuchtigkeit: " + (string)luftfeuchtigkeit.Attribute("value") + (string)luftfeuchtigkeit.Attribute("unit") +
                                      "\nWetter: " + (string)wolken.Attribute("value") + "\nRegenwahrscheinlichkeit: " + (string)wolken.Attribute("all") + (string)wolken.Attribute("unit"));
                }
                else
                {
                    Console.WriteLine("Zu Ihren Angaben wurden keine Wetterdaten gefunden \n\n");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
}
Posted by: Guest on November-03-2020

C# Answers by Framework

Browse Popular Code Answers by Language