Async-Await func return throws a System.FormatExceprion

42 Views Asked by At

I've been using these lines of code to get Currency from site and when I return value it throws a System.FormatExceprion (curr != null on return)

        public static async Task<double> GetCurrencyVal(string url)
        {
            string xPath = "//*[@id=\"__next\"]/div[2]/div[2]/section/div[2]/div/main/form/div[2]/div[1]/p[2]/text()[1]";
            var config = Configuration.Default.WithDefaultLoader();
            var doc = await BrowsingContext.New(config).OpenAsync(url).ConfigureAwait(continueOnCapturedContext: false);
            await doc.WaitForReadyAsync().ConfigureAwait(continueOnCapturedContext: false);
            var curr = doc.DocumentElement.SelectSingleNode(xPath);
            if (curr != null)
                return double.Parse(curr.Text().Trim());
            else
                Console.WriteLine("Error");
            return -1;
        }

        private void TimerOnTick(object sender = null, EventArgs eventArgs = null)
        {
            var now = DateTime.Now.AddSeconds(-DateTime.Now.Second);
            double usd = GetCurrencyVal(someUrl).GetAwaiter().GetResult();
            double eur = GetCurrencyVal(someUrl).GetAwaiter().GetResult();
        }

this line throws a System.FormatException

double usd = GetCurrencyVal(someUrl).GetAwaiter().GetResult();
1

There are 1 best solutions below

1
On

curr.Text().Trim() has contained "." as separator between int and fractional part, so trying to Parse it to double caused a System.FormatException