XML-Wetterdaten als RSS-Feed von Yahoo! abrufen

Links

Beispielcode

private void btnReadRSS_Click(object sender, EventArgs e)
{
    string sTempVal = string.Empty;

    string sLanguage = string.Empty;

    string sUnitTemperature = string.Empty;
    string sUnitDistance = string.Empty;
    string sUnitPressure = string.Empty;
    string sUnitSpeed = string.Empty;

    // p=GMXX0007 -> LocationID von Berlin
    // u=c -> metrisches System (°C, km/h) verwenden
    string sURLRSSFeed = "http://weather.yahooapis.com/forecastrss?p=GMXX0007&u=c";

    // Wetterdaten als RSS-Feed (XML) von Yahoo! lesen    
    XmlDocument xmlRSS = new XmlDocument();
    xmlRSS.Load(new XmlTextReader(sURLRSSFeed));

    // Daten aus XML lesen und in die RichTextBox eintragen
    rtbContent.Clear();

    // General
    rtbContent.AppendText("General:\n");
    rtbContent.AppendText("* Title:\t" + xmlRSS["rss"]["channel"]["title"].InnerText + "\n");
    rtbContent.AppendText("* Link:\t" + xmlRSS["rss"]["channel"]["link"].InnerText + "\n");
    rtbContent.AppendText("* Description:\t" + xmlRSS["rss"]["channel"]["description"].InnerText + "\n");
    sLanguage = xmlRSS["rss"]["channel"]["language"].InnerText;
    rtbContent.AppendText("* Language:\t" + sLanguage + "\n");
    rtbContent.AppendText("* LastBuildDate:\t" + xmlRSS["rss"]["channel"]["lastBuildDate"].InnerText + "\n");
    
    // Item
    rtbContent.AppendText("\nItem:\n");
    rtbContent.AppendText("* Title:\t" + xmlRSS["rss"]["channel"]["item"]["title"].InnerText + "\n");
    rtbContent.AppendText("* Link:\t" + xmlRSS["rss"]["channel"]["item"]["link"].InnerText + "\n");
    rtbContent.AppendText("* Latitude:\t" + xmlRSS["rss"]["channel"]["item"]["geo:lat"].InnerText + "\n");
    rtbContent.AppendText("* Longitude:\t" + xmlRSS["rss"]["channel"]["item"]["geo:long"].InnerText + "\n");
    rtbContent.AppendText("* Publishing date:\t" + xmlRSS["rss"]["channel"]["item"]["pubDate"].InnerText + "\n");
    
    // Location
    rtbContent.AppendText("\nLocation:\n");
    rtbContent.AppendText("* City:\t" + xmlRSS["rss"]["channel"]["yweather:location"].Attributes["city"].Value + "\n");
    rtbContent.AppendText("* Region:\t" + xmlRSS["rss"]["channel"]["yweather:location"].Attributes["region"].Value + "\n");
    rtbContent.AppendText("* Country:\t" + xmlRSS["rss"]["channel"]["yweather:location"].Attributes["country"].Value + "\n");
    
    // Units
    rtbContent.AppendText("\nUnits:\n");
    sUnitTemperature = "°" + xmlRSS["rss"]["channel"]["yweather:units"].Attributes["temperature"].Value;
    sUnitDistance = xmlRSS["rss"]["channel"]["yweather:units"].Attributes["distance"].Value;
    sUnitPressure = xmlRSS["rss"]["channel"]["yweather:units"].Attributes["pressure"].Value;
    sUnitSpeed = xmlRSS["rss"]["channel"]["yweather:units"].Attributes["speed"].Value;
    rtbContent.AppendText("* Temperature:\t" + sUnitTemperature + "\n");
    rtbContent.AppendText("* Distance:\t" + sUnitDistance + "\n");
    rtbContent.AppendText("* Pressure:\t" + sUnitPressure + "\n");
    rtbContent.AppendText("* Speed:\t" + sUnitSpeed + "\n");
    
    // Condition
    rtbContent.AppendText("\nCondition:\n");
    rtbContent.AppendText("* Text:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:condition"].Attributes["text"].Value + "\n");
    sTempVal = xmlRSS["rss"]["channel"]["item"]["yweather:condition"].Attributes["code"].Value;
    rtbContent.AppendText("* Code:\t" + sTempVal + " (" + ConditionCodeToString(sTempVal) + ")\n");
    rtbContent.AppendText("* Temp:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:condition"].Attributes["temp"].Value + sUnitTemperature + "\n");
    rtbContent.AppendText("* Date:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:condition"].Attributes["date"].Value + "\n");
    
    // Wind
    rtbContent.AppendText("\nWind:\n");
    rtbContent.AppendText("* Chill:\t" + xmlRSS["rss"]["channel"]["yweather:wind"].Attributes["chill"].Value + sUnitTemperature + "\n");
    rtbContent.AppendText("* Direction:\t" + xmlRSS["rss"]["channel"]["yweather:wind"].Attributes["direction"].Value + "°" + "\n");
    rtbContent.AppendText("* Speed:\t" + xmlRSS["rss"]["channel"]["yweather:wind"].Attributes["speed"].Value + " " + sUnitSpeed + "\n");
    
    // Atmosphere
    rtbContent.AppendText("\nAtmosphere:\n");
    rtbContent.AppendText("* Humidity:\t" + xmlRSS["rss"]["channel"]["yweather:atmosphere"].Attributes["humidity"].Value + "%" + "\n");
    float fVisibility = float.Parse(xmlRSS["rss"]["channel"]["yweather:atmosphere"].Attributes["visibility"].Value, System.Globalization.CultureInfo.CreateSpecificCulture(sLanguage));
    fVisibility /= 100.0f;
    rtbContent.AppendText("* Visibility:\t" + fVisibility.ToString() + sUnitDistance + "\n");
    rtbContent.AppendText("* Pressure:\t" + xmlRSS["rss"]["channel"]["yweather:atmosphere"].Attributes["pressure"].Value + sUnitPressure + "\n");
    sTempVal = xmlRSS["rss"]["channel"]["yweather:atmosphere"].Attributes["rising"].Value;
    rtbContent.AppendText("* Rising:\t" + sTempVal + " (" + RisingCodeToString(sTempVal) + ")\n");

    // Astronomy
    rtbContent.AppendText("\nAstronomy:\n");
    rtbContent.AppendText("* Sunrise:\t" + xmlRSS["rss"]["channel"]["yweather:astronomy"].Attributes["sunrise"].Value + "\n");
    rtbContent.AppendText("* Sunset:\t" + xmlRSS["rss"]["channel"]["yweather:astronomy"].Attributes["sunset"].Value + "\n");
    
    // Forecast current day
    rtbContent.AppendText("\nForecast current day:\n");
    rtbContent.AppendText("* Day:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].Attributes["day"].Value + "\n");
    rtbContent.AppendText("* Date:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].Attributes["date"].Value + "\n");
    rtbContent.AppendText("* Low:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].Attributes["low"].Value + sUnitTemperature + "\n");
    rtbContent.AppendText("* High:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].Attributes["high"].Value + sUnitTemperature + "\n");
    rtbContent.AppendText("* Text:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].Attributes["text"].Value + "\n");
    sTempVal = xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].Attributes["code"].Value;
    rtbContent.AppendText("* Code:\t" + sTempVal + " (" + ConditionCodeToString(sTempVal) + ")\n");
    
    // Forecast next day
    rtbContent.AppendText("\nForecast next day:\n");
    rtbContent.AppendText("* Day:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].NextSibling.Attributes["day"].Value + "\n");
    rtbContent.AppendText("* Date:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].NextSibling.Attributes["date"].Value + "\n");
    rtbContent.AppendText("* Low:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].NextSibling.Attributes["low"].Value + sUnitTemperature + "\n");
    rtbContent.AppendText("* High:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].NextSibling.Attributes["high"].Value + sUnitTemperature + "\n");
    rtbContent.AppendText("* Text:\t" + xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].NextSibling.Attributes["text"].Value + "\n");
    sTempVal = xmlRSS["rss"]["channel"]["item"]["yweather:forecast"].NextSibling.Attributes["code"].Value;
    rtbContent.AppendText("* Code:\t" + sTempVal + " (" + ConditionCodeToString(sTempVal) + ")\n");
    
    // Description (simple HTML)
    rtbContent.AppendText("\nDescription (simple HTML):\n");
    rtbContent.AppendText(xmlRSS["rss"]["channel"]["item"]["description"].InnerText + "\n");
}
/// <summary>
/// konvertiert den Code für Rising (0,1,2) zu einem string
/// </summary>
/// <param name="sRisingCode">0, 1, 2</param>
/// <returns>string</returns>
private string RisingCodeToString(string sRisingCode)
{
    string sRC = string.Empty;

    switch (sRisingCode)
    {
        case "0": sRC = "steady";
            break;
        case "1": sRC = "rising";
            break;
        case "2": sRC = "falling";
            break;
    }

    return sRC;
}
/// <summary>
/// konvertiert Condition zu string
/// </summary>
/// <param name="sConditionCode">Condition</param>
/// <returns>string</returns>
private string ConditionCodeToString(string sConditionCode)
{
    string sCC = string.Empty;

    switch (sConditionCode)
    {
        case "0": sCC = "tornado";
            break;
        case "1": sCC = "tropical storm";
            break;
        case "2": sCC = "hurricane";
            break;
        case "3": sCC = "severe thunderstorms";
            break;
        case "4": sCC = "thunderstorms";
            break;
        case "5": sCC = "mixed rain and snow";
            break;
        case "6": sCC = "mixed rain and sleet";
            break;
        case "7": sCC = "mixed snow and sleet";
            break;
        case "8": sCC = "freezing drizzle";
            break;
        case "9": sCC = "drizzle";
            break;
        case "10": sCC = "freezing rain";
            break;
        case "11": sCC = "showers";
            break;
        case "12": sCC = "showers";
            break;
        case "13": sCC = "snow flurries";
            break;
        case "14": sCC = "light snow showers";
            break;
        case "15": sCC = "blowing snow";
            break;
        case "16": sCC = "snow";
            break;
        case "17": sCC = "hail";
            break;
        case "18": sCC = "sleet";
            break;
        case "19": sCC = "dust";
            break;
        case "20": sCC = "foggy";
            break;
        case "21": sCC = "haze";
            break;
        case "22": sCC = "smoky";
            break;
        case "23": sCC = "blustery";
            break;
        case "24": sCC = "windy";
            break;
        case "25": sCC = "cold";
            break;
        case "26": sCC = "cloudy";
            break;
        case "27": sCC = "mostly cloudy (night)";
            break;
        case "28": sCC = "mostly cloudy (day)";
            break;
        case "29": sCC = "partly cloudy (night)";
            break;
        case "30": sCC = "partly cloudy (day)";
            break;
        case "31": sCC = "clear (night)";
            break;
        case "32": sCC = "sunny";
            break;
        case "33": sCC = "fair (night)";
            break;
        case "34": sCC = "fair (day)";
            break;
        case "35": sCC = "mixed rain and hail";
            break;
        case "36": sCC = "hot";
            break;
        case "37": sCC = "isolated thunderstorms";
            break;
        case "38": sCC = "scattered thunderstorms";
            break;
        case "39": sCC = "scattered thunderstorms";
            break;
        case "40": sCC = "scattered showers";
            break;
        case "41": sCC = "heavy snow";
            break;
        case "42": sCC = "scattered snow showers";
            break;
        case "43": sCC = "heavy snow";
            break;
        case "44": sCC = "partly cloudy";
            break;
        case "45": sCC = "thundershowers";
            break;
        case "46": sCC = "snow showers";
            break;
        case "47": sCC = "isolated thundershowers";
            break;
        case "3200": sCC = "not available";
            break;
    }

    return sCC;
}