コード例 #1
0
ファイル: ParsingXML.cs プロジェクト: pro100ham/SoftLine
        public List <ExchangeRateDTO> DownloadRatesFile()
        {
            try
            {
                string now = DateTime.UtcNow.Date.ToString("dd'/'MM'/'yyyy");
                string url = String.Format(@"http://www.cbr.ru/scripts/XML_daily.asp?date_req={0}", now);
                string xml;
                Log.AddLog("Try download currency");

                using (var webClient = new WebClient())
                {
                    xml = webClient.DownloadString(url);
                }

                XDocument doc = XDocument.Parse(xml);

                string date = doc.Element("ValCurs").Attribute("Date").Value;

                List <ExchangeRateDTO> rates = (from i in doc.Element("ValCurs").Elements("Valute")
                                                where i.Element("CharCode").Value == "USD" ||
                                                i.Element("CharCode").Value == "EUR" ||
                                                i.Element("CharCode").Value == "UZS"
                                                select new ExchangeRateDTO
                {
                    IsoCode = i.Element("CharCode").Value,
                    Nominal = i.Element("Nominal").Value,
                    ExchangeValue = i.Element("Value").Value,
                    RelevanceDate = date
                }).ToList();

                return(rates);
            }
            catch (Exception ex)
            {
                Log.AddLog(string.Format("Somethink is wrong in ParceXML: {0}", ex.Message));
                return(null);
            }
        }
コード例 #2
0
        public void UpdateCurrency()
        {
            try
            {
                var XmlDataList         = new ParsingXML().DownloadRatesFile();
                var XmlDataListNational = new ParsingXML().getNationCurrency();
                if (XmlDataList == null)
                {
                    Log.AddLog("Second attempt");
                    XmlDataList = new ParsingXML().DownloadRatesFile();
                    if (XmlDataList == null)
                    {
                        Log.AddLog("Bad Second attempt");
                        return;
                    }
                }
                if (XmlDataListNational == null)
                {
                    Log.AddLog("Second attempt /2");
                    XmlDataListNational = new ParsingXML().getNationCurrency();
                    if (XmlDataListNational == null)
                    {
                        Log.AddLog("Bad Second attempt /2");
                        return;
                    }
                }


                DateTime date = Convert.ToDateTime(XmlDataList.First().RelevanceDate);

                decimal USD, EUR, UZS, USDNational, EURNational, RUBNational;

                USD = Convert.ToDecimal(XmlDataList.Find(x => x.IsoCode.Contains("USD")).ExchangeValue) / Convert.ToDecimal(XmlDataList.Find(x => x.IsoCode.Contains("USD")).Nominal);
                EUR = Convert.ToDecimal(XmlDataList.Find(x => x.IsoCode.Contains("EUR")).ExchangeValue) / Convert.ToDecimal(XmlDataList.Find(x => x.IsoCode.Contains("EUR")).Nominal);
                UZS = Convert.ToDecimal(XmlDataList.Find(x => x.IsoCode.Contains("UZS")).ExchangeValue) / Convert.ToDecimal(XmlDataList.Find(x => x.IsoCode.Contains("UZS")).Nominal);

                USDNational = Convert.ToDecimal(XmlDataListNational.Find(y => y.IsoCode.Contains("USD")).ExchangeValue);
                EURNational = Convert.ToDecimal(XmlDataListNational.Find(y => y.IsoCode.Contains("EUR")).ExchangeValue);
                RUBNational = Convert.ToDecimal(XmlDataListNational.Find(y => y.IsoCode.Contains("RUB")).ExchangeValue);

                if (FindAndDeactivateEntity(date))
                {
                    var newRateUSD = new sl_ExchangeRate()
                    {
                        sl_RelevanceDate         = date,
                        sl_TransactionCurrencyId = ToTransactionCurrency("USD", decimal.One),
                        sl_ExchangeRate1         = decimal.One,
                        new_national_currency    = USDNational
                    };
                    service.Create(newRateUSD);
                    var newRateEUR = new sl_ExchangeRate()
                    {
                        sl_RelevanceDate         = date,
                        sl_TransactionCurrencyId = ToTransactionCurrency("EUR", EUR / USD),
                        sl_ExchangeRate1         = EUR / USD,
                        new_national_currency    = EURNational
                    };
                    service.Create(newRateEUR);
                    var newRateUZS = new sl_ExchangeRate()
                    {
                        sl_RelevanceDate         = date,
                        sl_TransactionCurrencyId = ToTransactionCurrency("UZS", UZS / USD),
                        sl_ExchangeRate1         = UZS / USD,
                        new_national_currency    = decimal.One
                    };
                    service.Create(newRateUZS);
                    var newRateRUB = new sl_ExchangeRate()
                    {
                        sl_RelevanceDate         = date,
                        sl_TransactionCurrencyId = ToTransactionCurrency("RUB", decimal.One / USD),
                        sl_ExchangeRate1         = decimal.One / USD,
                        new_national_currency    = RUBNational
                    };
                    service.Create(newRateRUB);
                }
                else
                {
                    Log.AddLog("All Currency is correct");
                }
            }
            catch (Exception ex)
            {
                Log.AddLog(string.Format("Somethink is wrong in WorkClass: {0}", ex.Message));
            }
        }
コード例 #3
0
 public void AddLog(string mess)
 {
     mainService.AddLog(mess);
 }