Aktuelle Währungskurse der EZB mit LINQ to XML  
Frank Dzaebel, erstellt am: 20.4.2008, zuletzt geändert:  20.4.2008
Kategorie: LINQ, .NET-Version: 3.5, [Download]

Aktuelle Währungskurse sind über die Europäische Zentralbank (EZB) zum Beispiel als XML-Datei downloadbar. Hier wird gezeigt, wie man mit LINQ to XML und C# 3.0 diese Datei recht übersichtlich auslesen kann.
Es gibt bereits ältere Projekte, wie hier (.NET 1.1), in denen auch noch Links und andere Möglichkeiten zu finden sind.
Die Kurse werden zur Übersicht kurz in einem DataGridView dargestellt:
Bild Währungskurs-Applikation

using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Globalization;

namespace CurrencyInfo
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    /// <summary>Tägliche Währungskurse (Mo-Fr) der Europäischen Zentralbank</summary>
    string uriDailyXml = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
    
    private void Form1_Load(object sender, EventArgs e)
    {
      XElement cubes = XElement.Load(uriDailyXml);
      var cubs =
        from el in cubes.Descendants()
        where el.Name.LocalName == "Cube" &&
              el.Attribute("time") != null
        select new
        {
          Datum = DateTime.Parse(el.Attribute("time").Value),
          Raten =
            from d in el.Descendants()
            select new
            {
              Name = CurrencyName(d.Attribute("currency").Value),
              ISOCode = d.Attribute("currency").Value,
              Rate = XmlConvert.ToDouble(d.Attribute("rate").Value)
            }
        };

      var kurse = cubs.ToList()[0];

      FlowLayoutPanel fp = new FlowLayoutPanel();
      fp.Dock = DockStyle.Fill; Controls.Add(fp);
      Label lbl = new Label(); lbl.Text = "Währungskurse vom:";
      lbl.AutoSize = true; lbl.Margin = lblPadding;
      fp.Controls.Add(lbl);

      TextBox tb = new TextBox(); tb.Width = txtWidth;
      tb.Text = kurse.Datum.ToLongDateString();
      fp.Controls.Add(tb);

      DataGridView dgv = new DataGridView();
      dgv.DataSource = kurse.Raten.ToList();
      dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
      fp.Controls.Add(dgv); dgv.Width = Width - widthAbzug;
      dgv.Height = Height - heightAbzug; 
    }

    const int heightAbzug = 70;
    const int widthAbzug = 20;
    const int txtWidth = 140;
    Padding lblPadding = new Padding(5, 6, 0, 0);

    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

    protected string CurrencyName(string isoCode)
    {
      // .NET Codes sind hier nicht 100% aktuell:
      // http://www.oanda.com/site/help/iso_code.shtml
      switch (isoCode)
      {
        case "BGN": isoCode = "BGL"; break;
        case "RON": isoCode = "ROL"; break;
        case "RUB": isoCode = "RUR"; break;
        default: break;
      }
      foreach (CultureInfo ci in cultures)
      {
        RegionInfo ri = new RegionInfo(ci.LCID);
        if (ri.ISOCurrencySymbol == isoCode) return ri.CurrencyEnglishName;
      }
      return null;
    }
  }
}