1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Xml;
namespace MaxPro.Common.Wetter
{
//========================================================================================================================
/// <summary>
/// Schnittstelle zum Weather Underground, wunderground.com
/// </summary>
public static class Wunderground
{
//------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Struktur für die Ergebnis-Daten
/// </summary>
public struct WundergroundForecastData
{
/// <summary>
/// Tagesdatum
/// </summary>
public DateTime Datum;
/// <summary>
/// Wetter-Vorhersage
/// </summary>
public string Condition;
/// <summary>
/// Bezeichnung für das dazustellende Icon
/// </summary>
public string Icon;
/// <summary>
/// Tiefste Temperatur
/// </summary>
public int TemperatureLow;
/// <summary>
/// Höchste Temperatur
/// </summary>
public int TemperatureHigh;
}
//------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Wettervorhersage für einen Ort herunterladen
/// </summary>
/// <param name="ort"></param>
/// <returns></returns>
public static IEnumerable<WundergroundForecastData> GetForecast(string ort)
{
// Ergebnis
List<WundergroundForecastData> ergebnis = new List<WundergroundForecastData>();
// Erstmal einen Webclient holen
WebClient web = new WebClient();
// XML Service von Wunderground
string url = "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=" + ort;
string xmlWunderground;
try
{
using (StreamReader sr = new StreamReader(web.OpenRead(url)))
{
xmlWunderground = sr.ReadToEnd();
sr.Close();
}
}
catch (Exception)
{
return null;
}
// XML auslesen
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlWunderground);
XmlElement root = doc.DocumentElement;
if (root != null)
{
XmlNode simpleForecast = root["simpleforecast"];
if (simpleForecast != null)
{
// Alle Elemente der Auflistung durcharbeiten
foreach (XmlNode forecastDay in simpleForecast)
{
// Ist es wirklich ein Forecast?
if (forecastDay.Name == "forecastday")
{
// Ergebnis einfügen
WundergroundForecastData wfd = new WundergroundForecastData();
// Datum aus der Datei lesen
XmlNode dateNode = forecastDay["date"];
if (dateNode != null)
wfd.Datum = new DateTime(dateNode["year"].GetInnerTextInt(), dateNode["month"].GetInnerTextInt(), dateNode["day"].GetInnerTextInt());
// Temperatur
XmlNode lowTemp = forecastDay["low"];
if (lowTemp != null)
wfd.TemperatureLow = lowTemp["celsius"].GetInnerTextInt();
XmlNode highTemp = forecastDay["low"];
if (highTemp != null)
wfd.TemperatureHigh = highTemp["celsius"].GetInnerTextInt();
// Condition
wfd.Condition = forecastDay["conditions"].GetInnerText();
wfd.Icon = forecastDay["icon"].GetInnerText();
// Ergebnis speichern
ergebnis.Add(wfd);
}
}
}
}
// Ergebnis
return ergebnis;
}
//------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// InnerText eines XmlElement als String zurückgeben
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private static string GetInnerText(this XmlElement node)
{
if (node != null)
return node.InnerText;
return "";
}
//------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// InnerText eines XmlElement als Integer zurückgeben
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private static int GetInnerTextInt(this XmlElement node)
{
try
{
if (node != null)
return Convert.ToInt32(node.InnerText);
}
catch (Exception) { }
return 0;
}
}
}
|