Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Html Agility Pack-通过行和列循环_C#_Html Agility Pack - Fatal编程技术网

C# Html Agility Pack-通过行和列循环

C# Html Agility Pack-通过行和列循环,c#,html-agility-pack,C#,Html Agility Pack,我刚刚开始使用C语言和HtmlAgilityPack解析一些html文件 我试图为每一行获取两个列的值,以便将它们插入到数据库中。 但运行以下程序: foreach (HtmlNode row in htmlDoc.DocumentNode.SelectNodes("//tr")) { foreach (HtmlNode cell in row.SelectNodes("//td")) { Console.WriteLine(cell.InnerText);

我刚刚开始使用C语言和HtmlAgilityPack解析一些html文件

我试图为每一行获取两个列的值,以便将它们插入到数据库中。 但运行以下程序:

foreach (HtmlNode row in htmlDoc.DocumentNode.SelectNodes("//tr"))
    {
    foreach (HtmlNode cell in row.SelectNodes("//td"))
    {
    Console.WriteLine(cell.InnerText);
    }
    }
当我在所有td上循环时,我得到了一个错误,而不仅仅是当前tr中包含的td

我的html如下所示:

<table>
        <tr>            
            <th align="center" width="50"><b>column 1</b></th>          
            <th align="center" width="210"><b>column 2</b></th>                 

        </tr>   



            <tr bgcolor="#ffffff">
                <td align="left"> </td>             
                <td align="left"></td>                          

            </tr>   


            <tr bgcolor="#dddddd">
                <td align="left"> </td>             
                <td align="left"></td>                          

            </tr>   


            <tr bgcolor="#ffffff">
                <td align="left"> </td>             
                <td align="left"></td>                          

            </tr>

第1栏
第2栏

我只是想让您知道,我已经更新了代码,该代码运行良好,但看起来一点也不好:

j = htmlDoc.DocumentNode.SelectNodes("//tr").Count;
                    if (j != 0)
                    {

                        for (int i = 2; i < j; ++i)
                        {
                            for (int k = 1; k < 3; k++)
                            {
                                HtmlNodeCollection row = htmlDoc.DocumentNode.SelectNodes("/html/body/table/tr[" + i + "]/td[" + k + "]");
                                Console.WriteLine("nb or row" + row.Count);


                                Console.WriteLine(row[0].InnerText);
                                //Console.Read(); 
                            }
                        }
                    }
j=htmlDoc.DocumentNode.SelectNodes(“//tr”).Count;
如果(j!=0)
{
对于(int i=2;i
如果您有任何想法,以加强这段代码,这将是伟大的

谢谢, 达米恩

也许是这个

var rows = doc.DocumentNode
              .SelectNodes("//tr")
              .Select((z, i) => new
                  {
                    RowNumber = i,
                    Cells = z.ChildNodes.Where(c => c.NodeType == HtmlNodeType.Element) })
              .ToList();

rows.ForEach(row => Console.WriteLine("{0}: {1}", row.RowNumber, string.Join(", ", row.Cells.Select(z => z.InnerText))));

你犯了什么错误?我没有错。但我希望每行只得到两列。相反,每次迭代我都会检索该文档的所有行部分