Blog Post by Murat Yaşar


Posted on Saturday, May 22, 2010 3:39:23 PM and it has been read 2648 times since then.


Indicative Exchange Rates Announced by the Central Bank of Turkey For Daily Basis

(TÜRKİYE CUMHURİYET MERKEZ BANKASI KURLARI)

I needed exchange rates in one of my projects. I searched on the internet and found many different techniques to accomplish this. Then I decided to write my own one. (Yes, reinventing the wheel again)

Anyway, I wanted to use some LINQ to XML syntax for this and turned out the amount of code I had to write became very short. The XML source I retrieve data from has some irregularities but you are the captain who can do any tricks or better to say that desired output. You can download complete solution as a zip file. The download link is at the end of this writing.

Have a great day.

public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!Page.IsPostBack)
      {
         try
         {
            DataTable result = getCurrencyRates();

            if (result != null)
            {
               if (result.Rows.Count > 0)
               {
                  GridView grid = new GridView();

                  grid.DataSource = result;

                  grid.DataBind();

                  divCurrency.Controls.Add(grid);
               }
            }
         }
         catch
         {
            divCurrency.InnerHtml = @"An error occurred while trying to get exchange rates.";
         }
      }
   }

   public DataTable getCurrencyRates()
   {
      string dateValue = XDocument.Load("http://www.tcmb.gov.tr/kurlar/today.xml").Element("Tarih_Date").Attribute("Tarih").Value;
		
      var resultSet = from result in XDocument.Load("http://www.tcmb.gov.tr/kurlar/today.xml").Descendants("Currency")
            .Where(result => result.Descendants("Isim").Any())
            .Where(result => result.Descendants("CurrencyName").Any())
            .Where(result => result.Descendants("ForexBuying").Any())
            .Where(result => result.Descendants("ForexSelling").Any())
            .Where(result => result.Descendants("BanknoteBuying").Any())
            .Where(result => result.Descendants("BanknoteSelling").Any())
            select new
            {
               Adi = result.Element("Isim").Value,
               Kod = result.Element("CurrencyName").Value,
               DovizAlis = result.Element("ForexBuying").Value,
               DovizSatis = result.Element("ForexSelling").Value,
               EfektifAlis = result.Element("BanknoteBuying").Value,
               EfektifSatis = result.Element("BanknoteSelling").Value
            };
		
            DataTable dt = new DataTable();
            DataRow dr;		 

            dt.Columns.Add(new DataColumn("Tarih", typeof(string))); //Date
            dt.Columns.Add(new DataColumn("Döviz Adı", typeof(string))); //CurrencyName
            dt.Columns.Add(new DataColumn("Kod", typeof(string))); //CurrencyCode
            dt.Columns.Add(new DataColumn("Doviz Alış", typeof(string))); //ForexBuying
            dt.Columns.Add(new DataColumn("Döviz Satış", typeof(string))); //ForexSelling
            dt.Columns.Add(new DataColumn("Efektif Alış", typeof(string))); //BanknoteBuying
            dt.Columns.Add(new DataColumn("Efektif Satış", typeof(string))); //BanknoteSelling

            foreach (var currency in resultSet)
            {
               dr = dt.NewRow();
               dr[0] = dateValue;
               dr[1] = currency.Adi;
               dr[2] = currency.Kod;
               dr[3] = currency.DovizAlis;
               dr[4] = currency.DovizSatis;
               dr[5] = currency.EfektifAlis;
               dr[6] = currency.EfektifSatis;
               dt.Rows.Add(dr);
            }

            return dt;
      }
}

 


- Downloadable File/s -
File Name File Size Description
exchangeratescentralbankofturkey.zip 28.392 KB Exchange Rates Announced by the Central Bank of Turkey For Daily Basis Download

(In order to use this feature, you have to register.)

Tag Related Blog Entries

Scratching Beneath the Surface

Friday, March 11, 2022 0   2555  

API For Getting Indicative Exchange Rates From Central Bank of the Republic of Turkey

Saturday, September 01, 2018 1   3543   1

Downloading And Parsing XML File Using Python & Tkinter

Tuesday, November 08, 2016 0   4716  

TCMB Mobile Applications

Friday, January 15, 2016 1   4959  

Successfully Going Live With The New yasarmurat.com

Tuesday, May 26, 2015 0   3707  

The first windows phone 8 app sumbitted to the Windows Phone Store

Saturday, August 03, 2013 3   7493  

Some Helpful Links For Software Developers

Saturday, April 28, 2012 0   8394  

File Is Being Used By Another Process

Monday, August 29, 2011 1   4108  

Populate Nested TreeView In Asp.Net Using Common Table Expression In SQL Server

Sunday, May 01, 2011 0   8816   5

DataBinding GridView In Asp.NET Application By Using PostgreSQL as DataSource

Sunday, October 10, 2010 0   12467  

LINQ

Friday, October 08, 2010 0   3549  

Asp.Net Application Warm Up By Using Windows Service

Thursday, February 04, 2010 6   15530   6

Tuning, Optimizing, Increasing and Improving Performance of Asp.Net Application - Part III

Saturday, January 23, 2010 0   4516  

Tuning, Optimizing, Increasing and Improving Performance of Asp.Net Application - Part II

Saturday, January 23, 2010 0   8970  

Tuning, Optimizing, Increasing and Improving Performance of Asp.Net Application - Part I

Saturday, January 23, 2010 1   6866  

Syntax not understood error for robots.txt file in Google Webmaster Tools

Tuesday, December 29, 2009 2   5296