Diver Dan
Diver Dan

Reputation: 9963

extract data from an html tbody using c#

I am using c# Web.Client to download an html string.

A small example of the html been returned is

 <tbody class='resultBody ' id='Tbody2'>
        <tr id='Tr2' class='firstRow'>
            <td class='cbrow tier_Gold' rowspan='4'>
                <input type='checkbox' name='listingId' value='452' id='Checkbox2' />
            </td>
            <td class='resNum' rowspan='4'>
                <div class='node'>
                    B</div>
            </td>
            <td class='datarow busName' id='Td2'>

            </td>
            <td rowspan='2' class='resLinks'>
            </td>
            <td class="hoops" rowspan='2'>
            </td>
        </tr>
        <tr>
            <td class="datarow">
                <dl class="addrBlock">
                    <dd class="bizAddr">
                        123 ABC St</dd>
                    </dl>
            </td>
        </tr>
    </tbody>
    <tbody class='resultBody ' id='Tbody3'>
        <tr id='Tr3' class='firstRow'>
            <td class='cbrow tier_Gold' rowspan='4'>
                <input type='checkbox' name='listingId' value='99' id='Checkbox3' />
            </td>
            <td class='resNum' rowspan='4'>
                <div class='node'>
                    B</div>
            </td>
            <td class='datarow busName' id='Td3'>

            </td>
            <td rowspan='2' class='resLinks'>
            </td>
            <td class="hoops" rowspan='2'>
            </td>
        </tr>
        <tr>
            <td class="datarow">
                <dl class="addrBlock">
                    <dd class="bizAddr">
                        1111 Some St</dd>
                    </dl>
            </td>
        </tr>
    </tbody>

I am interested in 2 elements of the html but I have no idea the best way to get to them. How would be the best way for me to get the value from and get the inner html from the element

Any suggestions would be great!!!

Upvotes: 3

Views: 1479

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064114

  • download the HTML Agility Pack (free)
  • create a new HtmlDocument
  • loadhtml
  • use DOM navigation or an xpath query (SelectSingleNode etc) to find the elements
  • access InerHtml of the elements you want

The API is similar to XmlDocument, but it works on html that isn't xhtml.

Upvotes: 3

Related Questions