コード例 #1
0
        /// <summary>
        /// Prints the fxRates in the corresponding rows : columns
        /// Rows represent dates
        /// Columns represent currencies
        /// </summary>
        /// <param name="activeWorksheet">Excel:Worksheet: worksheet where the data gets copied</param>
        private void PrintFxRates(Excel.Worksheet activeWorksheet)
        {
            foreach (CurrencyWrapper currencyWrapper in CurrenciesRetrieved)
            {
                foreach (Currency currency in currencyWrapper.CurrencyRates)
                {
                    FxDateWrapper currencySnapshotRef = CurrencySnapshotDate.FirstOrDefault(x => x.Date == currency.Date);

                    Excel.Range fxCellReference = activeWorksheet.Cells[currencySnapshotRef.RowReferece, currencyWrapper.ColumnReference];
                    var         normRawRate     = currency.RawRate.Replace(',', '.');
                    fxCellReference.Value = normRawRate;
                    ((dynamic)fxCellReference).NumberFormat = "0.00";
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Query the MNB Client to retrieve Exchange rates for given currencies
        /// </summary>
        /// <param name="start">DateTime: The start date to be used in the query</param>
        /// <param name="end">DateTime: The end date to be used in the query</param>
        /// <param name="currencyNames">IList<string>: The names of the currencies to be used in the query</param>
        private void GetAndProcessExchangeRates(DateTime start, DateTime end, IList <string> currencyNames)
        {
            try
            {
                string startDate = start.ToString(DATE_REQUEST_FORMAT);
                string endDate   = end.ToString(DATE_REQUEST_FORMAT);

                var currencies = string.Join(",", currencyNames);

                GetExchangeRatesRequestBody requestBody = new GetExchangeRatesRequestBody()
                {
                    startDate     = startDate,
                    endDate       = endDate,
                    currencyNames = currencies
                };

                GetExchangeRatesResponseBody exchangeRatesResponseBody = client.GetExchangeRates(requestBody);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(exchangeRatesResponseBody.GetExchangeRatesResult);

                XmlNodeList rawNodes = xmlDoc.SelectNodes(MNB_EXCHANGE_RATES_PER_DAY);
                foreach (XmlNode rawNode in rawNodes)
                {
                    // Retrieve the day of the FX rate and store it in variable: date
                    XmlNode rawDate = rawNode.Attributes.GetNamedItem(MNB_DATE_ATTRIBUTE_NAME);
                    DateTime.TryParse(rawDate.Value, out DateTime date);

                    // check if date is not included
                    var dateIsAlreadyAdded = CurrencySnapshotDate.Any(item => item.Date == date);
                    if (!dateIsAlreadyAdded)
                    {
                        var newFxDate = new FxDateWrapper()
                        {
                            Date = date,
                        };
                        CurrencySnapshotDate.Add(newFxDate);
                    }

                    // Iterate through child nodes and retrieve the unit and curr from attributes - and fx rate from innerText
                    foreach (XmlNode currencyRate in rawNode.ChildNodes)
                    {
                        // Find currency by name
                        XmlNode currAttrib      = currencyRate.Attributes.GetNamedItem(MNB_CURRENCY_ATTRIBUTE_NAME);
                        var     currencyWrapper = CurrenciesRetrieved.FirstOrDefault(currName => currName.CurrencyName.Equals(currAttrib.Value, StringComparison.InvariantCultureIgnoreCase));

                        if (currencyWrapper is null)
                        {
                            continue;
                        }

                        // feed currency unit to currencyWrapper
                        XmlNode unitAttrib = currencyRate.Attributes.GetNamedItem(MNB_UNIT_ATTRIBUTE_NAME);
                        if (Int32.TryParse(unitAttrib.Value, out int result))
                        {
                            currencyWrapper.RateUnit = result;
                        }

                        // Feed rate into currencyWrapper
                        currencyWrapper.CurrencyRates.Add(new Currency()
                        {
                            Date    = date,
                            RawRate = currencyRate.InnerText
                        });
                    }
                }
            }
            catch (Exception e)
            {
                string error = e.Message;
                ShowErrorMessage(error);
            }
        }