This project has moved and is read-only. For the latest updates, please go here.

Selecting specific childs of childs of childs...?

Feb 17, 2011 at 9:02 AM

Hello all!


I'm new in using HAP and so far have been impressed (dear lord, no more regex no more!). I've done quite a few things but here's one I can't find how to do it.


I have a table, with a row, which has another table within that row, and that row has the usual rows and cells, from which I need to extract the info. Of course the HTML is not mine. To clarify things, the HTML code goes like this:


<table cellpadding=0 cellspacing=0 border="0" width="100%">
<tr><td class="matchStyle">
<table style="border: solid #3972b9 1px" cellspacing="0" cellpadding="1" width="100%">
  <tr height="15">
    <td class="matchStyle" height="15"><b></b></td>
    <td style="text-align:center" class="matchStyle">Indoor Hard</td>
    <td style="text-align: right" class="matchStyle"><b></b></td>
  <tr height="15">
    <td class="matchStyleHead" height="15">&nbsp;</td>
    <td style="text-align:center" class="matchStyleHead"><b>Match Statistics</b></td>
    <td class="matchStyleHead">&nbsp;</td>
  <tr height="15">
    <td class="matchStyle" height="15">70%    (74-31)</td>
    <td style="text-align:center" class="matchStyle">Match W/L</td>
    <td style="text-align: right" class="matchStyle">48%    (13-14)</td>
  <tr height="15">
    <td class="matchStyle" height="15">69%    (162-74)</td>
    <td style="text-align:center" class="matchStyle">Set W/L</td>
    <td style="text-align: right" class="matchStyle">47%    (30-34)</td>
  <tr height="15">
    <td class="matchStyle" height="15">57%    (1312-1002)</td>
    <td style="text-align:center" class="matchStyle">Gm W/L</td>
    <td style="text-align: right" class="matchStyle">51%    (332-325)</td>

I'd need to extact each groups of 3 cells thathave the percentages and the name of it to create a list names with the corresponding values.

I first tried to do this:
            var linksOnPage = from lnks in tempHtml.DocumentNode.Descendants("tr")
                              where lnks.Name == "td" &&
                                    lnks.GetAttributeValue("class", null) == "matchStyle" &&
                                    lnks.InnerText.Trim().Length > 0
                              select new
                                  Text = lnks.InnerText

But it didn't bring nothing. As it was the first time I put something on Descendants(), I removed it thinking about getting all <td> first and the I'd filter, but has some weird results.
Is there any way to go directly where I want to and query the childs in a levels like <table> -> <tr> -> <td> -> <table> -> <tr> -> <td> ?

Thanks a lot for the help and the developers for this amazing piece of software!


Jan 12, 2012 at 9:02 AM

Did you solved your problem?


I am facing the same problem where Descendants is not returning grandchildren nodes!!!!

I am using now:

var tds= doc.DocumentNode.DescendantNodes().Where(n => n.Name.ToLower() == "td");
But maybe there is a better way?????