From İzmir to the digital world.

Fetching CBRT Exchange Rates with C#

Web & Software 3 min read

Hello! In this article I will try to explain how to load an XML document from the internet into a DataTable and how to display the loaded data in a DataGrid.

The CBRT has finally started publishing its exchange rates in an XML document. Anyone who has dealt with this before knows that the rates used to be scraped from an HTML document called today.html with a primitive method, so your work was at the mercy of the webmaster. Fortunately, those days are over: now there is today.xml. It is still in a trial phase and – in my opinion – has some logic errors, but it is great that they have switched to the XML standard. The address of the page: http://www.tcmb.gov.tr/kurlar/today.xml I recommend studying this document thoroughly first.

Let’s get straight to the application.

Create a new Windows application and add a DataGrid to your form. Don’t forget to add the System.Xml namespace to your code.

private void Form1_Load(object sender, System.EventArgs e)
{
dataGrid1.DataSource=source();
}
public DataTable source ()
{
DataTable dt = new DataTable();
// We create our DataTable object
DataRow dr;
// We define the rows of the DataTable.
dt.Columns.Add(new DataColumn(“Name”, typeof(string)));
dt.Columns.Add(new DataColumn(“Code”, typeof(string)));
dt.Columns.Add(new DataColumn(“Forex buying”, typeof(string)));
dt.Columns.Add(new DataColumn(“Forex selling”, typeof(string)));
dt.Columns.Add(new DataColumn(“Banknote buying”, typeof(string)));
dt.Columns.Add(new DataColumn(“Banknote selling”, typeof(string)));
// We add 6 columns to our DataTable and define their data types.
XmlTextReader rdr = new XmlTextReader(“http://www.tcmb.gov.tr/kurlar/today.xml“);
// We create the XmlTextReader object and pass the URL of the XML document as a parameter
// XmlTextReader provides fast, forward-only access to the XML document at the given URL.
XmlDocument myxml = new XmlDocument();
// We create the XmlDocument object.
myxml.Load(rdr);
// We load the XML with the Load method
XmlNode tarih = myxml.SelectSingleNode(“/Tarih_Date/@Tarih“);
XmlNodeList mylist = myxml.SelectNodes(“/Tarih_Date/Currency”);
XmlNodeList adi = myxml.SelectNodes(“/Tarih_Date/Currency/Isim”);
XmlNodeList kod = myxml.SelectNodes(“/Tarih_Date/Currency/@Kod“);
XmlNodeList doviz_alis = myxml.SelectNodes(“/Tarih_Date/Currency/ForexBuying”);
XmlNodeList doviz_satis = myxml.SelectNodes(“/Tarih_Date/Currency/ForexSelling”);
XmlNodeList efektif_alis = myxml.SelectNodes(“/Tarih_Date/Currency/BanknoteBuying”);
XmlNodeList efektif_satis = myxml.SelectNodes(“/Tarih_Date/Currency/BanknoteSelling”);
// We define each node as an XmlNodeList by passing the XPath of the nodes
// as a parameter to the SelectSingleNode method.
dataGrid1.CaptionText=tarih.InnerText.ToString()+” – central bank exchange rates”;
// We set the caption of the DataGrid.
int x = 19;
/* Because of the – in my opinion – logic error in the XML that I mentioned, I defined a variable such as x.
This x = 19 makes sure that only 19 rows are added to the DataTable, because after the 19th node of the XML document
there are no current exchange rates but Euro conversion rates, and from this node on the structure does not match the first 18 nodes.
In my view, they should have been kept in a separate XML document.
*/
for (int i = 0 ; i < x; i++)
{
dr = dt.NewRow();
dr[0] = adi.Item(i).InnerText.ToString(); // content of the i-th adi node
// We fill the rows of the DataColumn called Name with the /Tarih_Date/Currency/Isim nodes.
dr[1] = kod.Item(i).InnerText.ToString();
// Code rows
dr[2] = doviz_alis.Item(i).InnerText.ToString();
// Forex buying
dr[3] = doviz_satis.Item(i).InnerText.ToString();
// Forex selling
dr[4] = efektif_alis.Item(i).InnerText.ToString();
// Banknote buying
dr[5] = efektif_satis.Item(i).InnerText.ToString();
// Banknote selling.
dt.Rows.Add(dr);
}
// We fill our DataTable with 18 rows
// although once again we are at the webmaster’s mercy :).
// the problem I mentioned above.
return dt;
// We return the DataTable.
}

And there are the latest exchange rates in our application!

I have also written a cross-rate conversion program that can serve as an example of XML querying. I plan to tidy it up and publish it as soon as possible.

Note:

XmlTextReader rdr = new XmlTextReader(” http://www.tcmb.gov.tr/kurlar/today.xml“);
DataSet ds = new DataSet();
ds.ReadXml(rdr);
dataGrid1.DataSource=ds;

This code loads the XML document at the given URL directly into a DataSet. In other words, these 4 lines do the same job as the code above :)

Services, projects, products and knowledge base