R4nc1d
R4nc1d

Reputation: 3123

Get Custom Html tag using HTML Agility Pack c#

Given I have the following HTML, how can I use HTML Agility Pack to get the value 18.2099

I had a look at the following How get a custom tag with html agility pack? but that did not work.

<div class="last-price">
    <div class="last u-up">
        <bdo class="last-price-value js-streamable-element" dir="ltr" data-s="100">18.2099</bdo>
    </div>
</div>

Current Code

var htmlText = await response.Content.ReadAsStringAsync(cancellationToken);
    
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlText);

var htmlTestNodes = htmlDoc.DocumentNode.SelectNodes("//dbo"); <-- This returns null
var htmlNode = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='last u-up']/dbo[@class='last-price-value']");
if (decimal.TryParse(htmlNode.InnerText, out var value))
{
    return value;
}

Upvotes: 0

Views: 114

Answers (1)

Bayram Eren
Bayram Eren

Reputation: 442

this might work

var htmlNode = document.DocumentNode.SelectSingleNode("/html/body/div/div/bdo");

if (decimal.TryParse(htmlNode.InnerText, out var value))
{
    Console.WriteLine(value);
}

Or

var htmlNode = document.DocumentNode.SelectSingleNode("//bdo[@class='last-price-value js-streamable-element']");

if (decimal.TryParse(htmlNode.InnerText, out var value))
{
    Console.WriteLine(value);
}

Upvotes: 2

Related Questions