Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static decimal ConvertCurrency(Dictionary<string, decimal> snapshot, string from, string to, decimal value)
- {
- if (from == "USD")
- {
- decimal valueUsd = snapshot[to] * value;
- return valueUsd;
- }
- else
- {
- decimal valueUsd = (1m / snapshot[from]) * value;
- decimal valueTo = snapshot[to] * valueUsd;
- return valueTo;
- }
- }
- public static Dictionary<string, decimal> ExchangeRateSnapshot()
- {
- Dictionary<string, decimal> db = new Dictionary<string, decimal>();
- string url = "http://finance.yahoo.com/webservice/" + "v1/symbols/allcurrencies/quote?format=xml";
- try
- {
- // Load the data.
- XmlDocument doc = new XmlDocument();
- doc.Load(url);
- // Process the resource nodes.
- XmlNode root = doc.DocumentElement;
- string xquery = "descendant::resource[@classname='Quote']";
- foreach (XmlNode node in root.SelectNodes(xquery))
- {
- const string name_query =
- "descendant::field[@name='name']";
- const string price_query =
- "descendant::field[@name='price']";
- string name =
- node.SelectSingleNode(name_query).InnerText;
- string price =
- node.SelectSingleNode(price_query).InnerText;
- decimal inverse = 1m / decimal.Parse(price);
- if (name.StartsWith("USD/"))
- if (!db.ContainsKey(name.Replace("USD/", "")))
- {
- db.Add(name.Replace("USD/", ""), decimal.Parse(price));
- }
- }
- }
- catch (Exception ex)
- { }
- return db;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement