0

I'm trying to extract information from within a DIV that contains all between 2-6 tables with HtmlAgilityPack.

As long as there are the same amount of tables I can get the information without any problems by using

var thirdTable = res.SelectSingleNode("table[3]");

Each table contains tds and the first one contains the name I want to check for. At the moment it's done by

<table border="0" cellspacing="1" cellpadding="4" width="100%">
    <tbody>
        <tr bgcolor="#505050">
            <td colspan="2" class="white">
                <b>Character Deaths</b>
            </td>
        </tr>
    </tbody>
</table>

var charTest = thirdTable.ChildNodes[0].InnerText.StartsWith(checkInfo);

but since the amount of tables differs from time to time I cannot extract the information without having a NullReferenceException.

How can I check the amount of tables then tell which tables that exists?

EDIT:

       var URLCharacter = "http://www..." + tbSearch.Text;
       HtmlAgilityPack.HtmlWeb doc = new HtmlAgilityPack.HtmlWeb();
       htmldocObject = doc.Load(URLCharacter);
       //Find the right contentdiv
       var res = htmldocObject.DocumentNode.SelectSingleNode("//div[@class='BoxContent']");
2
  • Do you want to select the text "Character Deaths" ? In other words, Do you want to select the text in the 1st cell of all tables, within an element represented by res?
    – Zasz
    Aug 3, 2012 at 17:19
  • I want to check the amount of tables there are and then check which "header" they have, because within there's a lot more data that I want to extract. The problem I'm facing is that there are not always the same amount of tables within the div.
    – noxious
    Aug 3, 2012 at 18:02

1 Answer 1

0

What I would do is attempt to get the third table by selecting all tables, skipping the first two, then take the next one (the third). If you get an element, do what you want with it, otherwise do nothing.

var thirdTable = res.Elements("table").Skip(2).FirstOrDefault();
if (thirdTable != null)
{
    // do stuff
}

If you're having trouble using the Elements() method, you could use SelectNodes() with appropriate xpath for a replacement.

var thirdTable = res.SelectNodes("table").Skip(2).FirstOrDefault();
6
  • For some reason I'm getting the error Method not found: 'Int32 System.Environment.get_CurrentManagedThreadId()'. on this line "var thirdTable = res.Elements("table").Skip(2).FirstOrDefault();"
    – noxious
    Aug 4, 2012 at 13:45
  • ive researched some, currently im using .net framework 4 (without client profiling), do I need a newer version?
    – noxious
    Aug 4, 2012 at 16:27
  • No that should work with 3.5 and up however due to that exception, targeting .net 4.5 should resolve it. That exception is new to me. I see that call to Environment.CurrentManagedThreadId which is a .net 4.5 thing. Is that a call you're making or is that being called by HAP? It would have to be removed/replaced if you're targeting .net 4. It could be coming from however you generated res. Would you mind including the code for that? Aug 4, 2012 at 17:25
  • I've edited now, however that variable has worked all the time before while I've been trying to work it out, maybe does not work together with elements. It also seems that the "Elements" part is causing it as I've tried without the Skip() and FirstOrDefault(). cheers
    – noxious
    Aug 4, 2012 at 18:27
  • You know, it could be the build of HAP that you're using. I noticed that the latest versions target .net 4.5 so you probably shouldn't be using them. Or maybe it could be considered a bug? You could either target .net 4.5 or get a version of HAP that targets .net 4. Either way, the Elements() call can be replaced with the equivalent SelectNodes() call. Aug 4, 2012 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.