다음 표가 있습니다.
<table>
<tr><th>header1</th><th>header2</th><th>header3</th></tr>
<tr><td>value01</td><td>value02</td><td>value03</td></tr>
<tr><td>value11</td><td>value12</td><td>value13</td></tr>
<tr>
<td colspan="3">
<table>
<tr><td>subvalue01</td><td>subvalue02</td></tr>
</table>
</td>
</tr>
</table>
이 코드를 사용하여 주 테이블 셀 값을 별도의 ArrayList
및 다른 ArrayList
하위 셀 값에 저장합니다. 그러나 하위 셀 값에 대한 내 ArrayList
는 테이블과 하위 테이블을 포함하여 전체 값을 저장합니다.
foreach (HtmlNode table in hdoc.DocumentNode.SelectNodes("//table"))
{
///This is the table.
foreach (HtmlNode row in table.SelectNodes("tr").Skip(1))
{
///This is the row.
foreach (HtmlNode cell in row.SelectNodes("th|td"))
///can also use "th|td", but right now we ONLY need td
{
//This is the cell.
if (cell.InnerHtml.Contains("<table>"))
{
foreach (HtmlNode subtable in cell.SelectNodes("//table"))
{
foreach (HtmlNode subrow in subtable.SelectNodes("tr").Skip(1))
{
foreach (HtmlNode subcell in subrow.SelectNodes("th|td"))
{
arrSubList.Add(subcell.InnerText);
}
}
}
}
else
{
arrList.Add(cell.InnerText);
}
}
}
}
내 코드에 어떤 문제가 있습니까?
나는 너의 첫줄을 믿는다.
foreach (HtmlNode table in hdoc.DocumentNode.SelectNodes("//table"))
모든 레벨 (중첩 테이블 포함)에서 모든 테이블을 선택합니다.
Per : http://www.w3schools.com/XPath/xpath_syntax.asp
// 위치에 관계없이 선택 영역과 일치하는 현재 노드에서 문서의 노드를 선택합니다.
첫 줄을 다음과 같이 변경하십시오.
foreach (HtmlNode table in hdoc.DocumentNode.SelectNodes("/html/body/table"))
그리고 그것이 어떻게되는지보십시오.