Select specific html with "Html Agility pack"

c# html-agility-pack

Question

I'm using html-agility-pack and trying to select out a specific html in it. The part I want to get is every GTIN-number in these blocks:

<td><span class="mobile-only">GTIN:</span>07330155011068</td>

-The part I want is the numbers after the ending span-tag. Ex: 07330155011068. Below is my html, and my c#-method:

<div class="table-wrapper" style='display: block;'>
    <table id="tableSearchArticle">
        <thead>
            <tr>
                <th><a href="#">Article</a></th>
                <th><a href="#">art.nr.</a></th>
                <th><a href="#">Brand</a></th>
                <th><a href="#">GTIN</a></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <a href="http://www.dabas.com/ProductSheet/Detail.ashx/121308" target="_blank">
                        Dalapannkaka fryst ca100st 6kg
                    </a>
                </td>
                <td><span class="mobile-only">Tillverkarens art.nr:</span>11068</td>
                <td><span class="mobile-only">Varumärke:</span>test</td>
                <td><span class="mobile-only">GTIN:</span>07330155011068</td>
            </tr>
            <tr>
                <td>
                    <a href="http://www.dabas.com/ProductSheet/Detail.ashx/124494" target="_blank">
                        Dessertpannkaka fryst ca100st 6kg
                    </a>
                </td>
                <td><span class="mobile-only">Tillverkarens art.nr:</span>11405</td>
                <td><span class="mobile-only">Varumärke:</span>test</td>
                <td><span class="mobile-only">GTIN:</span>07330155114059</td>
            </tr>
        </tbody>
    </table>

    </div>

And I'm using this method to trying to get my values. The problem is I don't know what code to write in the SelectNode() to get the innerHtml containing the GTIN-numbers.

public void TestGetHtml()
    {
        var doc = new HtmlDocument();
        doc.Load("C:/Users/Desktop/test.html");
        foreach (HtmlNode link in doc.DocumentNode.SelectNodes("TODO: Add code to select all GTIN"))
        {

        }
        doc.Save("file.htm");
    }

Accepted Answer

Use Xpath to select fourth cells from body of table with id tableSearchArticle. Then get inner text of cells (it will be without html tags, like GTIN:07330155114059) and remove GTIN prefix:

var xpath = "//table[@id='tableSearchArticle']/tbody/tr/td[4]";
var gtins = doc.DocumentNode.SelectNodes(xpath)
               .Select(td => td.InnerText.Replace("GTIN:", ""));

Output:

[
  "07330155011068",
  "07330155114059"
]

Popular Answer

SelectNodes receives an Xpath expression. So, you could start with this (untested):

foreach (HtmlNode tr in doc.DocumentNode.SelectNodes(
   "//div[@class='table-wrapper']/table[@id='tableSearchArticle']/tbody/tr"))
{
    Console.WriteLine(tr.InnerHtml);
    Console.WriteLine(tr.SelectSingleNode(".//a").GetAttribute("href"));
    Console.WriteLine(tr.SelectSingleNode(".//td[last()]").InnerText);
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why