迭代每个表并获取信息

本文关键字:获取 信息 迭代 | 更新日期: 2023-09-27 18:25:48

我需要从两个表中获取信息:

http://pastebin.com/BzbKPH2i

我迭代每个表,并从每个表中获得td的信息

但我得到了两次信息,orderingText被填充了x2,所以为了防止第二次迭代,我设置了break来阻止它,但我认为这不是HtmlAgilityPack的工作方式,或者我做了错误的

// Get the node from the div
//
HtmlNode orderingProcNode = docProcSpecs.DocumentNode.SelectSingleNode("//div[@id='orderingordering']");
string[] orderingText = new string[1024];
int t = 0;
// Iterate each table
foreach (HtmlNode orderingProcessor in orderingProcNode.SelectNodes("//table[@class='noSort infoTable']"))
{
    foreach (HtmlNode ordProcessor in orderingProcessor.SelectNodes("//tbody//tr//td"))
    {
        // Here I get all the info from the two tables instead of one table
        //
        orderingText[t++] = ordProcessor.InnerText.Trim();
    }
    break; // this is the solution
}

迭代每个表并获取信息

问题是内部foreach循环-您在文档中选择所有tbody节点,而您真正想要的是当前节点的所有子节点-只需删除斜杠:

foreach (HtmlNode ordProcessor in orderingProcessor.SelectNodes("tbody/tr/td"))