如何从特定单元格C#Html-Agility-Pack中获取值 [英] How to get the value from a specific cell C# Html-Agility-Pack
问题描述
如何从文档第二个表中的特定位置获取值.我需要下面的html文档中第二个单元格向下和第三列中的值.我该怎么做.
How do I get a value from a specific location in the second table in the document. I need the value from the second cell down and third column over in the html document below. How do I do this.
<html>
<head>
<title>Tables</title>
</head>
<body>
<table border="1">
<tr>
<th>Room</th>
<th>Location</th>
</tr>
<tr>
<td>Paint</td>
<td>A4</td>
</tr>
<tr>
<td>Stock</td>
<td>B3</td>
</tr>
<tr>
<td>Assy</td>
<td>N9</td>
</tr>
</table>
<p></p>
<table border="1">
<tr>
<th>Product</th>
<th>Mat'l</th>
<th>Weight</th>
<th>Size</th>
</tr>
<tr>
<td>Cover</td>
<td>Plastic</td>
<td>4</td>
<td>16</td>
</tr>
<tr>
<td>Retainer</td>
<td>Steel</td>
<td>12</td>
<td>8</td>
</tr>
<tr>
<td>Pin</td>
<td>Bronze</td>
<td>18</td>
<td>7</td>
</tr>
</table>
<p></p>
<table border="1">
<tr>
<th>Process</th>
<th>Location</th>
<th>Number</th>
</tr>
<tr>
<td>Trim</td>
<td>S2</td>
<td>8</td>
</tr>
<tr>
<td>Finish</td>
<td>D2</td>
<td>3</td>
</tr>
</table>
</body>
</html>
谢谢!
也...请帮助新手!!! 请引导我到可以帮助我理解Html-Agility-Pack(HAP)语法的资源.我有用于HAP的CHM文件-我尝试过使用它,并且尝试将VS的对象浏览器用于HAP,但这时对我来说太神秘了.
Also... Please help a newbie out!!! Please direct me to a resource that can help me understand the syntax of Html-Agility-Pack (HAP). I have the CHM file for HAP - I've tried to use it and I've tried to use VS's object browser for HAP, but it's too cryptic for me at this point.
推荐答案
HTML Agility Pack配备了XPATH评估程序,该评估程序遵循.NET XPATH语法在已解析的HTML节点上.请注意,与此库一起使用的XPATH表达式要求元素和属性名称必须小写,而与原始HTML源代码无关.
Html Agility Pack is equipped with an XPATH evaluator that follows .NET XPATH syntax over the parsed HTML nodes. Note the XPATH expression used with this library require elements and attribute names to be lowercase, independently from the original HTML source.
因此,在您的情况下,您可以使用以下表达式获取第三列,第二行,第二个表的单元格:
So in your case, you can get the cell for the 3rd column, 2nd row, 2nd table with an expression like this:
HtmlDocument doc = new HtmlDocument();
doc.Load(YouTestHtmlFilePath);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//table[2]/tr[2]/td[3]");
Console.WriteLine(node.InnerText); // will output "4"
//table
表示从根递归获取任何TABLE元素. [2]
表示进入第二张桌子.
//table
means get any TABLE element recursively from root. [2]
means take the 2nd table.
/tr
表示从此当前表中获取任何TR元素. [2]
表示进入第二行.
/tr
means get any TR element from this current table. [2]
means take the 2nd row.
/td
表示从此当前行获取任何TD元素. [3]
表示进入第三个单元格.
/td
means get any TD element from this current row. [3]
means take the 3nd cell.
您可以在此处找到优秀的XPATH教程: XPath教程
You can find good XPATH tutorials here: XPath Tutorial
这篇关于如何从特定单元格C#Html-Agility-Pack中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!