Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 在XPathC中获取所有td#_C#_Html_Xpath_Html Agility Pack - Fatal编程技术网

C# 在XPathC中获取所有td#

C# 在XPathC中获取所有td#,c#,html,xpath,html-agility-pack,C#,Html,Xpath,Html Agility Pack,我正在尝试使用C#中的HtmlAgilityPack解析HTML。我有21个tr项目,每个tr项目都有7个td项目。如何将所有tr和td项目按顺序排列?现在我只能得到一个tr项目和它的7个td项目 这是我的C#代码: 我尝试使用[@id=\“searchResultsTable\”]/tbody/tr[1]/td[position()尝试下面的代码(未测试。可能会出现编译错误。但这给了您一个想法。) 代码中的注释提供了更多详细信息 //GET THE TABLE NODE HtmlNode ta

我正在尝试使用C#中的HtmlAgilityPack解析HTML。我有
21个tr项目
,每个tr项目都有
7个td项目
。如何将所有tr和td项目按顺序排列?现在我只能得到一个tr项目和它的7个td项目

这是我的C#代码:

我尝试使用
[@id=\“searchResultsTable\”]/tbody/tr[1]/td[position()尝试下面的代码(未测试。可能会出现编译错误。但这给了您一个想法。)

代码中的注释提供了更多详细信息

//GET THE TABLE NODE
HtmlNode table = document.DocumentNode.SelectSingleNode("//*[@id='searchResultsTable']");

//LOOP THROUGH THE TABLE NODE AND FIND EACH TR 
foreach (HtmlNode row in table.SelectNodes("//tr")) {

      //PRINT HERE WHATEVER YOU WANT FOR EACH ROW.
      Console.WriteLine("New Row");

      //LOOP THROUGH THE ALL TD OF EACH TR
      foreach (HtmlNode cell in row.SelectNodes("//td")) {
          //PRINT HERE EACH TD
          Console.WriteLine("cell: " + cell.InnerText);
      } //END TD

}//END TR

与前面提到的类似,使用选择器查询以循环
tr
元素,然后选择每行的固定位置
td
节点:

假设结构如下:

<table id="searchResultsTable">
<tbody>
<tr>
    <td>1</td>
    <td>Name<a>Name 1</a></td>
    <td>Year 1</td>
    <td>KM 1</td>
    <td>Color 1</td>
    <td>Price 1</td>
    <td>Date 1</td>
    <td>Location 1</td>
</tr>
<tr>
    <td>2</td>
    <td>Name<a>Name 2</a></td>
    <td>Year 2</td>
    <td>KM 2</td>
    <td>Color 2</td>
    <td>Price 2</td>
    <td>Date 2</td>
    <td>Location 2</td>
</tr>
</tbody>
var document = new HtmlDocument();
document.Load("example.html");

var rows = document.DocumentNode.SelectNodes("//*[@id='searchResultsTable']/tbody/tr");

foreach(var row in rows)
{
    var name = row.SelectSingleNode("td[2]/a[1]").InnerText;
    var year = row.SelectSingleNode("td[3]").InnerText;
    var km = row.SelectSingleNode("td[4]").InnerText;
    var color = row.SelectSingleNode("td[5]").InnerText;
    var price = row.SelectSingleNode("td[6]").InnerText;
    var date = row.SelectSingleNode("td[7]").InnerText;
    var location = row.SelectSingleNode("td[8]").InnerText;

    Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}", name, year, km, color, price, date, location);
}
Name 1, Year 1, KM 1, Color 1, Price 1, Date 1, Location 1
Name 2, Year 2, KM 2, Color 2, Price 2, Date 2, Location 2
产生:

<table id="searchResultsTable">
<tbody>
<tr>
    <td>1</td>
    <td>Name<a>Name 1</a></td>
    <td>Year 1</td>
    <td>KM 1</td>
    <td>Color 1</td>
    <td>Price 1</td>
    <td>Date 1</td>
    <td>Location 1</td>
</tr>
<tr>
    <td>2</td>
    <td>Name<a>Name 2</a></td>
    <td>Year 2</td>
    <td>KM 2</td>
    <td>Color 2</td>
    <td>Price 2</td>
    <td>Date 2</td>
    <td>Location 2</td>
</tr>
</tbody>
var document = new HtmlDocument();
document.Load("example.html");

var rows = document.DocumentNode.SelectNodes("//*[@id='searchResultsTable']/tbody/tr");

foreach(var row in rows)
{
    var name = row.SelectSingleNode("td[2]/a[1]").InnerText;
    var year = row.SelectSingleNode("td[3]").InnerText;
    var km = row.SelectSingleNode("td[4]").InnerText;
    var color = row.SelectSingleNode("td[5]").InnerText;
    var price = row.SelectSingleNode("td[6]").InnerText;
    var date = row.SelectSingleNode("td[7]").InnerText;
    var location = row.SelectSingleNode("td[8]").InnerText;

    Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}", name, year, km, color, price, date, location);
}
Name 1, Year 1, KM 1, Color 1, Price 1, Date 1, Location 1
Name 2, Year 2, KM 2, Color 2, Price 2, Date 2, Location 2

我已经测试了你的代码。它不是“td”和“tr”,而是“//td”和“//tr”。否则它会卡在tbody中,找不到它们。有了这个更改,它就可以正常工作了。谢谢!解决方案正常工作了!有没有办法不在tr/td下获取div。
/*[@id=“searchResultsTable”]/tbody/tr[1]/td[2]/div[1]/div[1]/a[1]
我不想得到这些div。我只想得到tds。@katamarayudu@derloopkat@heyaacell.ChildNodes.First().InnerText将获取td根级别的文本,并忽略嵌套标记的内容。例如,1XXX年返回1年。但是,如果(cell.ChildNodes.Count>0),则需要检查td是否为空{//get text here}如何排除某些tr?在我的HTML中,tr[5]行中没有任何内容。因此,发生了NullReferenceException。我尝试了
//tr(*除了tr[5])
@derloopkat@heyaa,与我在td中提到的方法相同,您可以为tr添加一个IF语句来检查是否有子节点。另一个选项是只检查tr的内部文本是否为空。为了忽略空格,请使用Trim()。我尝试了你的解决方案。我不知道为什么,但我只得到3个tr。@JohnLI理解为什么我只得到3个tr。因为tr[4]是空的。