The HTMLAgilityPack ChildNodes index is functional, but the named node is not.

html-agility-pack selectsinglenode


I'm using HTMLAgilityPack to parse an XML API answer. I may choose the API call results that I want. After that, I want to write the ChildNodes to a table using a loop across the items. When I say anything like: "Select ChildNodes,"

sItemId = dnItem.ChildNodes(0).innertext

I get the correct itemId outcome. But as I attempt:

sItemId = dnItem.ChildNodes("itemId").innertext

"Referenced object has a value of 'Nothing'," is what I get.

I've tried a variety of strings, including "itemID[1]," "/itemId[1]," and more. SelectSingleNode and ChildNodes are something I've attempted. Item("itemId"). innertext. The index method is the only one that has been successful.

The issue with utilizing the index is that it may become inaccurate since child components are sometimes left out of the results.

Anyone know what I'm doing incorrectly?

3/11/2010 7:31:16 PM

Accepted Answer

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlDocument();
HtmlNodeCollection tableRows = htmlDoc.DocumentNode.SelectNodes("//tr");
for (int i = 0; i < tableRows.Count; i++)
    HtmlNode tr = tableRows[i];

    HtmlNode[] td = new HtmlNode[2];
    string xpath = tr.XPath + "//td";
    HtmlNodeCollection cellRows = tr.SelectNodes(@xpath);
    //td[0] = tr.ChildNodes[1];                
    //td[1] = tr.ChildNodes[3];
        td[0] = cellRows[0];
        td[1] = cellRows[1];
    catch (Exception)
    { }

Row by row and cell by cell, data is extracted from a table using the code. I modified the original xpath to suit my requirements using it as a starting point. Good fortune!

3/12/2010 3:32:31 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow