Search code examples
c#innerhtml

When parsing an element of a html document, the conversion is missing a proceeding 0


When I select an element of the html document, then convert it to an int64, its missing a preceding 0.

Using InnerHtml, I only want the last 4 characters from the "Prime Z790 BIOS 0812" result.

The result of this newz790 var should be 0812, but I just get 812.


namespace getsqldata
{
    public static class ASUSBIOSversions
    {
        public static long newz790;
        public static async Task Run()
            
        {

            //*****************Get BIOS info from website**************
            //Scrape Website
            {
                await GethtmlAsync();
                //Console.ReadLine();
            }
            async static Task GethtmlAsync()
            {

                //Get Url for scraping and create document for parsing
                var url = "https://www.asus.com/motherboards-components/motherboards/prime/prime-z790-p/helpdesk_bios/?model2Name=PRIME-Z790-P";


                var httpClient = new HttpClient();
                var html = await httpClient.GetStringAsync(url);

                var htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(html);

                await Task.Delay(5000); // Add a delay of 5 seconds

                //Specify element of page to be retrieved
                var biositem = htmlDocument.DocumentNode.SelectNodes("//div[contains(@class, 'ProductSupportDriverBIOS__fileTitle')]");
                

                //Grab the BIOS version              
                ASUSBIOSversions.newz790 = Convert.ToInt64(biositem[0].InnerHtml[^4..]);
              

Solution

  • Int64 is an integer, and does not contain leading 0's. You can add leading zeros when casting it to a string, if you want to...

    In64 Reference and using leading Zeros

    If you need the zero, and grabbing it from a "string" in innerHtml, why not keep it a string?