用于节点数组的 c# HtmlAgilityPack

c# HtmlAgilityPack for on nodes array

我正在使用 html 敏捷包,在我获得节点数组后:

HtmlNode[] nodes = document.DocumentNode.SelectNodes("//tbody[@class='table']").ToArray();

现在我想 运行 每个节点 [i] 一个 for 循环。我试过这个:

 for (int i = 0; i < 1; i++)
            {

                if (t == null)
                    t = new Model.Track();

                 HtmlNode[] itemText = nodes[i].SelectNodes("//td[@class='artist']").ToArray();

                for (int x = 0; x < itemText.Length; x++)
                { //doing something      }

问题是 itemtext 数组没有关注 nodes[i] 。 但是会在 html 文档中带出所有 ("//td[@class='artist']") 的数组。 有帮助吗?

使用 //td[@class='artist'] 将从您的 document.DocumentNode.

中获取具有 artist class 的所有列

使用 .//td[@class='artist'](注意开头的点)将从当前选定的节点中获取所有带有 artist class 的列,在您的情况下是 nodes[i] .