Example #1
0
        private static DateTime GetCurrencyRates(IDbUnit dbUnit, IEnumerable <Currency> currencies,
                                                 DateTime?startsWith = null)
        {
            var currentDateTime = DateTime.Now;
            var date1           = (startsWith ?? new DateTime(2016, 4, 1)).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
            var date2           = currentDateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

            foreach (var currency in currencies)
            {
                var doc = new XmlDocument();
                doc.Load($"http://www.cbr.ru/scripts/XML_dynamic.asp?date_req1={date1}&date_req2={date2}&VAL_NM_RQ={currency.ID}");
                var nodes = doc.SelectNodes("//Record");
                if (null == nodes)
                {
                    throw new XPathException();
                }

                dbUnit.CurrencyRates.AddRange(from node in nodes.Cast <XmlNode>()
                                              let date = DateTime.Parse(node?.Attributes?["Date"].Value, new CultureInfo("ru-Ru"))
                                                         let nominal                         = int.Parse(node["Nominal"]?.InnerText ?? "1")
                                                                                   let value = node["Value"]?.InnerText
                                                                                               where null != value
                                                                                               select new CurrencyRate()
                {
                    CurrencyID = currency.ID,
                    Currency   = currency,
                    Date       = date,
                    Value      = decimal.Parse(value, new CultureInfo("ru-Ru")) / nominal
                });
            }

            dbUnit.Complete();

            return(currentDateTime);
        }
Example #2
0
        private static ICollection <Currency> GetCurrencies(IDbUnit dbUnit)
        {
            var doc = new XmlDocument();

            doc.Load("http://www.cbr.ru/scripts/XML_val.asp?d=0");
            var nodes = doc.SelectNodes("//Item");

            if (null == nodes)
            {
                throw new XPathException();
            }

            var supportedCurrencies = ConfigurationManager.AppSettings["supported_currencies"].Split(';');
            var currencies          = (from node in nodes.Cast <XmlNode>()
                                       let currencyName = node["EngName"]?.InnerText
                                                          where supportedCurrencies.Contains(currencyName)
                                                          let currencyId = node["ParentCode"]?.InnerText.Trim()
                                                                           select new Currency()
            {
                ID = currencyId,
                Name = currencyName
            }).ToList();

            dbUnit.Currencies.AddRange(currencies);
            dbUnit.Complete();

            return(currencies);
        }