views:

317

answers:

1

I am parsing an XML API response with HTMLAgilityPack. I am able to select the result items from the API call. Then I loop through the items and want to write the ChildNodes to a table. When I select ChildNodes by saying something like:

sItemId = dnItem.ChildNodes(0).innertext

I get the proper itemId result. But when I try:

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

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

I have tried "itemID[1]", "/itemId[1]" and a veriety of strings. I have tried SelectSingleNode and ChildNodes.Item("itemId").innertext. The only one that has worked is using the index.

The problem with using the index is that sometimes child elements are omitted in the results and that throw off the index.

Anybody know what I am doing wrong?

A: 
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(@webHTMLeditor.TextXhtml);
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];
    try
    {
        td[0] = cellRows[0];
        td[1] = cellRows[1];
    }
    catch (Exception)
    { }
    //etc   
}

The code is used to extract data from a table, row by row, by cell per row. I used the existing xpath and I altered it acording to my needs. Good luck!

Meltdown
Are you using the full xpath in the SelectNodes of the tr? I am trying this and it is not working. I thought the ChildNodes or SelectNodes methods would use the relative XPath but I can do whichever. But it's not working here.
XgenX
Yes, I am using the full Xpath. It is the Xpath provided by the <tr>. It kinda looks like "//table[1]//tbody[1]//tr[1]" to which I add the "//td" to get al the TableData from that specific table row.I prefered this aproach to the aproach I commented out because I could'n guarantee that all the <tr>'s would have the same childred structure.
Meltdown
Also, If it is of any use to you try to use the HAP Explorerhttp://htmlagilitypack.codeplex.com/releases/view/33903#DownloadId=86142This way you can see how the xpath is constructed. Good luck!
Meltdown
When you talk about the HAPExplorer and say "how the xpath is constructed" do you mean the hierarchy? When I load the a document into it it shows the XML in the top box. Then click the parse button it shows the hierarchy in the bottom window. There is another button called "Test Code". When I click down on it it turns light gray and when I let up it turns back to dark gray. I don't know if that is a pass or fail.<searchResult><item><itemId>21</itemId></item><item><itemId>22</itemId></item></searchResult>If I want the itemId elements (assume there are other elements) what is path from root?
XgenX
Sorry about terseness. Had only 2 chr left. If oNode.xpath="//searchResult//item" and I was looping through oNodes what would ChildPaths or SelectSingleNodes xpath parameter look like?I have tried every combo of "//itemId", "itemId[1]", "itemId*", "/itemId/", "//itemId[1]" etc with and without full path that I have been able to imagine. I will keep trying while waiting for response.
XgenX
You can go to a specific node with something like //searchResult[1]//itemId[2]//item[1] for the 2nd item of the searchResult Parent and the first child of the itemId parent. The numbers can be generated with a FOR loop in C#, or any other programming language you are using.
Meltdown