public UsdExRate[] ConverttoUSD(string from, string amount)
        {
            /**
            * this section can be moved to constructor since its a shared.
            */
            Dictionary<string, float> CurrencyDic = cache["CurrencyDic"] as Dictionary<string, float>;
            if (CurrencyDic == null || CurrencyDic.Count == 0)
            {
                WebClient webClient = new WebClient();
                String url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml";
                String xml = webClient.DownloadString(url);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);

                XmlNodeList LastDateCurrencyNodes = xmlDoc.DocumentElement.ChildNodes[2].ChildNodes[0].ChildNodes;

                Dictionary<string, float> CurrencyDictionary = new Dictionary<string, float>();
                for (int i = 0; i < LastDateCurrencyNodes.Count; i++)
                {

                    CurrencyDictionary.Add((string)LastDateCurrencyNodes[i].Attributes["currency"].Value, float.Parse(LastDateCurrencyNodes[i].Attributes["rate"].Value));
                }
                CurrencyDictionary.Add("EUR", float.Parse("1.00"));
                policy = new CacheItemPolicy();
                DateTime today = DateTime.Now;
                DateTime expire = today.AddDays(1);
                policy.AbsoluteExpiration = expire;
                cache.Set("CurrencyDic", CurrencyDictionary, policy);

                if (CurrencyDictionary.ContainsKey(from) && CurrencyDictionary.ContainsKey("USD"))
                {
                    float convertedAmount = float.Parse(amount) * CurrencyDictionary[from] / CurrencyDictionary["USD"];
                    float exchangeRate = 1 / convertedAmount;
                    UsdExRate[] res = new UsdExRate[]{
                        new UsdExRate(){
                            exRate = exchangeRate.ToString()
                        }
                    };

                    return res;

                }
                else
                {
                    UsdExRate[] res = new UsdExRate[]{
                        new UsdExRate(){
                            exRate = "0.00000",
                            errorMsg = "Api does not support this operation"
                        }
                    };
                    return res;
                }

            }
            else
            {

                if (CurrencyDic.ContainsKey(from) && CurrencyDic.ContainsKey("USD"))
                {
                    float convertedAmount = float.Parse(amount) * CurrencyDic[from] / CurrencyDic["USD"];
                    float exchangeRate = 1 / convertedAmount;
                    UsdExRate[] res = new UsdExRate[]{
                        new UsdExRate(){
                            exRate = exchangeRate.ToString()
                        }
                    };

                    return res;
                }
                else
                {
                    UsdExRate[] res = new UsdExRate[]{
                        new UsdExRate(){
                            exRate = "0.00000",
                            errorMsg = "Api does not support this operation"
                        }
                    };
                    return res;
                }
            }
        }
Beispiel #2
0
        public UsdExRate[] ConverttoUSD(string from, string amount)
        {
            /**
             * this section can be moved to constructor since its a shared.
             */
            Dictionary <string, float> CurrencyDic = cache["CurrencyDic"] as Dictionary <string, float>;

            if (CurrencyDic == null || CurrencyDic.Count == 0)
            {
                WebClient   webClient = new WebClient();
                String      url       = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml";
                String      xml       = webClient.DownloadString(url);
                XmlDocument xmlDoc    = new XmlDocument();
                xmlDoc.LoadXml(xml);

                XmlNodeList LastDateCurrencyNodes = xmlDoc.DocumentElement.ChildNodes[2].ChildNodes[0].ChildNodes;


                Dictionary <string, float> CurrencyDictionary = new Dictionary <string, float>();
                for (int i = 0; i < LastDateCurrencyNodes.Count; i++)
                {
                    CurrencyDictionary.Add((string)LastDateCurrencyNodes[i].Attributes["currency"].Value, float.Parse(LastDateCurrencyNodes[i].Attributes["rate"].Value));
                }
                CurrencyDictionary.Add("EUR", float.Parse("1.00"));
                policy = new CacheItemPolicy();
                DateTime today  = DateTime.Now;
                DateTime expire = today.AddDays(1);
                policy.AbsoluteExpiration = expire;
                cache.Set("CurrencyDic", CurrencyDictionary, policy);


                if (CurrencyDictionary.ContainsKey(from) && CurrencyDictionary.ContainsKey("USD"))
                {
                    float       convertedAmount = float.Parse(amount) * CurrencyDictionary[from] / CurrencyDictionary["USD"];
                    float       exchangeRate    = 1 / convertedAmount;
                    UsdExRate[] res             = new UsdExRate[] {
                        new UsdExRate()
                        {
                            exRate = exchangeRate.ToString()
                        }
                    };


                    return(res);
                }
                else
                {
                    UsdExRate[] res = new UsdExRate[] {
                        new UsdExRate()
                        {
                            exRate   = "0.00000",
                            errorMsg = "Api does not support this operation"
                        }
                    };
                    return(res);
                }
            }
            else
            {
                if (CurrencyDic.ContainsKey(from) && CurrencyDic.ContainsKey("USD"))
                {
                    float       convertedAmount = float.Parse(amount) * CurrencyDic[from] / CurrencyDic["USD"];
                    float       exchangeRate    = 1 / convertedAmount;
                    UsdExRate[] res             = new UsdExRate[] {
                        new UsdExRate()
                        {
                            exRate = exchangeRate.ToString()
                        }
                    };


                    return(res);
                }
                else
                {
                    UsdExRate[] res = new UsdExRate[] {
                        new UsdExRate()
                        {
                            exRate   = "0.00000",
                            errorMsg = "Api does not support this operation"
                        }
                    };
                    return(res);
                }
            }
        }