Unten ist meine HTML-Seite:
Mit Html Agility Pack benötige ich die Main Table Main Rows. Bedeutet in der unteren Seite, nur eine Haupttabelle und nur 3 Hauptzeilen unter der Haupttabelle.
Ich muss die Zählung von 3 erhalten, ausgenommen alle inneren TR.
Bitte helfen Sie.
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Untitled Page</title>
</head>
<body style="width: 800">
<table width="700" style="background-repeat: no-repeat;">
<tr>
<td>
<table width="700">
<tr>
<td width="20%"></td>
<td width="60%" align="center" style="font-family: Arial; font-size: 12pt;"> SUMMARY </td>
<td width="20%"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="700">
<tr>
<td width="20%"></td>
<td width="60%" align="center" style="font-family: Arial; font-size: 12pt;"> SUMMARY </td>
<td width="20%"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="700">
<tr>
<td width="20%"></td>
<td width="60%" align="center" style="font-family: Arial; font-size: 12pt;"> SUMMARY </td>
<td width="20%"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Code:
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlFile);
int count = 1;
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
foreach (HtmlNode table in tables)
{
var rows = doc.DocumentNode.SelectNodes("//tr");
//here i need to get those 3 rows only.
//but instead i am getting all inner TR also.
foreach (HtmlNode tr in rows)
{
}
}
Sie wählen alle Tabellen aus.
Versuchen Sie, nur den Tisch auszuwählen, der ein Kind des Körpers ist, etwas wie:
SelectSingleNode("//body/table");
Zählen Sie dann die TRs in diesem Knoten.
Oder versuchen Sie alles in einem Schritt:
SelectNodes("//body/table/tr");