Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1.         public static decimal ConvertCurrency(Dictionary<string, decimal> snapshot, string from, string to, decimal value)
  2.         {
  3.             if (from == "USD")
  4.             {
  5.                 decimal valueUsd = snapshot[to] * value;
  6.                 return valueUsd;
  7.             }
  8.             else
  9.             {
  10.                 decimal valueUsd = (1m / snapshot[from]) * value;
  11.                 decimal valueTo = snapshot[to] * valueUsd;
  12.                 return valueTo;
  13.             }
  14.         }
  15.  
  16.         public static Dictionary<string, decimal> ExchangeRateSnapshot()
  17.         {
  18.             Dictionary<string, decimal> db = new Dictionary<string, decimal>();
  19.             string url = "http://finance.yahoo.com/webservice/" + "v1/symbols/allcurrencies/quote?format=xml";
  20.             try
  21.             {
  22.                 // Load the data.
  23.                 XmlDocument doc = new XmlDocument();
  24.                 doc.Load(url);
  25.  
  26.                 // Process the resource nodes.
  27.                 XmlNode root = doc.DocumentElement;
  28.                 string xquery = "descendant::resource[@classname='Quote']";
  29.                 foreach (XmlNode node in root.SelectNodes(xquery))
  30.                 {
  31.                     const string name_query =
  32.                         "descendant::field[@name='name']";
  33.                     const string price_query =
  34.                         "descendant::field[@name='price']";
  35.  
  36.                     string name =
  37.                         node.SelectSingleNode(name_query).InnerText;
  38.                     string price =
  39.                         node.SelectSingleNode(price_query).InnerText;
  40.                     decimal inverse = 1m / decimal.Parse(price);
  41.  
  42.                     if (name.StartsWith("USD/"))
  43.                         if (!db.ContainsKey(name.Replace("USD/", "")))
  44.                         {
  45.                             db.Add(name.Replace("USD/", ""), decimal.Parse(price));
  46.                         }
  47.                 }
  48.             }
  49.             catch (Exception ex)
  50.             { }
  51.  
  52.             return db;
  53.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement