我需要得到第二个表第 4 行的总和。我想从第二列开始求和。我该怎么做呢?
static void Main()
{
String htmlFile = "C:/Temp/Test_11.html";
HtmlDocument doc = new HtmlDocument();
doc.Load(htmlFile);
//var sum = doc.DocumentNode.SelectSingleNode("//table") // <<<< No error when I access the first table
var sum = doc.DocumentNode.SelectSingleNode("//table[2]") // <<<< Error when I try to access the 2nd table
.Elements("tr")
// Skip this many rows from the top
.Skip(1)
// .ElementAt(2) = third column
.Sum(tr => int.Parse(tr.Elements("td").ElementAt(2).InnerText));
Console.WriteLine(sum);
Console.ReadLine();
}
下面是由两个表组成的 html 文件。总和的结果应该是 26。
<html>
<head>
<title>Tables</title>
</head>
<body>
<table border="1">
<tr>
<th>Environment1</th>
<th>Databases</th>
<th>Sites</th>
<th>Site Collection Storage Used (GB)</th>
<th>Ref</th>
</th>
</tr>
<tr>
<td>Public1</td>
<td>14</td>
<td>28</td>
<td>32.6602</td>
<td>2</td>
</tr>
<tr>
<td>Local1</td>
<td>4</td>
<td>9</td>
<td>21.0506</td>
<td>1</td>
</tr>
<tr>
<td>Shared1</td>
<td>6</td>
<td>9</td>
<td>17.092</td>
<td>9</td>
</tr>
</table>
<p></p>
<table border="1">
<tr>
<th>Environment2</th>
<th>Databases</th>
<th>Sites</th>
<th>Site Collection Storage Used (GB)</th>
<th>Ref</th>
</th>
</tr>
<tr>
<td>Public2</td>
<td>15</td>
<td>13</td>
<td>31.5602</td>
<td>1</td>
</tr>
<tr>
<td>Local2</td>
<td>5</td>
<td>8</td>
<td>7.0302</td>
<td>3</td>
</tr>
<tr>
<td>Shared2</td>
<td>4</td>
<td>5</td>
<td>13.109</td>
<td>4</td>
</tr>
</table>
</body>
</html>
请在这件事上给予我帮助