HTML 민첩성 팩으로 테이블 열 삭제

asp.net c# html-agility-pack html-table

문제

나는 내 웹 사이트를 위해 C #을 사용하는 웹 사이트에서 테이블을 긁어서 문자열에로드했습니다. 너무 많은 열이 있으므로 일부를 삭제하는 쉬운 방법이 있는지, 아마도 HTML 민첩성 팩을 사용하지만 C #에서는 필요한 경우 궁금 해서요.

문자열의 테이블은 다음과 같습니다.

    <table>
        <tr>
            <th scope="col">&nbsp; </th>
            <th scope="col">&nbsp; </th>
            <th scope="col">P </th>
            <th scope="col">W </th>
            <th scope="col">L </th>
            <th scope="col">T </th>
            <th scope="col">NR </th>
            <th scope="col">Bat </th>
            <th scope="col">Bowl </th>
            <th scope="col">Pen </th>
            <th scope="col">Pts </th>
        </tr>
        <tr>
            <td>1 </td>
            <td><a href="fixbyteam.aspx?clubid=44576&teamid=58170&divid=32181">Rayleigh 2nd</a> </td>
            <td>12 </td>
            <td>8 </td>
            <td>1 </td>
            <td>0 </td>
            <td>3 </td>
            <td>14 </td>
            <td>52 </td>
            <td>0 </td>
            <td>209 </td>
        </tr>
        <tr>
            <td>2 </td>
            <td><a href="fixbyteam.aspx?clubid=44612&teamid=58169&divid=32181">Rainham 1st</a> </td>
            <td>12 </td>
            <td>8 </td>
            <td>1 </td>
            <td>1 </td>
            <td>2 </td>
            <td>12 </td>
            <td>56 </td>
            <td>-15 </td>
            <td>199 </td>
        </tr>
        <tr class="lineAbove">
            <td>3 </td>
            <td><a href="fixbyteam.aspx?clubid=44571&teamid=58162&divid=32181">Old Chelmsfordians 2nd</a> </td>
            <td>12 </td>
            <td>5 </td>
            <td>5 </td>
            <td>0 </td>
            <td>2 </td>
            <td>10 </td>
            <td>48 </td>
            <td>0 </td>
            <td>148 </td>
        </tr>
        <tr>
            <td>4 </td>
            <td><a href="fixbyteam.aspx?clubid=44570&teamid=58161&divid=32181">Little Baddow 2nd</a> </td>
            <td>12 </td>
            <td>5 </td>
            <td>4 </td>
            <td>0 </td>
            <td>3 </td>
            <td>21 </td>
            <td>43 </td>
            <td>-15 </td>
            <td>144 </td>
        </tr>
        <tr>
            <td>5 </td>
            <td><a href="fixbyteam.aspx?clubid=44606&teamid=58159&divid=32181">Rayne 1st</a> </td>
            <td>12 </td>
            <td>5 </td>
            <td>4 </td>
            <td>0 </td>
            <td>3 </td>
            <td>6 </td>
            <td>39 </td>
            <td>0 </td>
            <td>140 </td>
        </tr>
        <tr>
            <td>6 </td>
            <td><a href="fixbyteam.aspx?clubid=44605&teamid=58158&divid=32181">Terling 1st</a> </td>
            <td>12 </td>
            <td>4 </td>
            <td>5 </td>
            <td>1 </td>
            <td>2 </td>
            <td>12 </td>
            <td>35 </td>
            <td>0 </td>
            <td>129 </td>
        </tr>
        <tr>
            <td>7 </td>
            <td><a href="fixbyteam.aspx?clubid=44602&teamid=58154&divid=32181">Willow Herbs 1st</a> </td>
            <td>12 </td>
            <td>4 </td>
            <td>6 </td>
            <td>0 </td>
            <td>2 </td>
            <td>9 </td>
            <td>34 </td>
            <td>0 </td>
            <td>117 </td>
        </tr>
        <tr>
            <td>8 </td>
            <td><a href="fixbyteam.aspx?clubid=50925&teamid=68864&divid=32181">Ongar 1st</a> </td>
            <td>12 </td>
            <td>3 </td>
            <td>5 </td>
            <td>0 </td>
            <td>4 </td>
            <td>3 </td>
            <td>42 </td>
            <td>-5 </td>
            <td>108 </td>
        </tr>
        <tr class="lineAbove">
            <td>9 </td>
            <td><a href="fixbyteam.aspx?clubid=44607&teamid=58163&divid=32181">Sandon Sports 1st</a> </td>
            <td>12 </td>
            <td>3 </td>
            <td>6 </td>
            <td>0 </td>
            <td>3 </td>
            <td>8 </td>
            <td>27 </td>
            <td>0 </td>
            <td>98 </td>
        </tr>
        <tr>
            <td>10 </td>
            <td><a href="fixbyteam.aspx?clubid=44582&teamid=58156&divid=32181">Little Waltham 2nd</a> </td>
            <td>12 </td>
            <td>1 </td>
            <td>9 </td>
            <td>0 </td>
            <td>2 </td>
            <td>14 </td>
            <td>25 </td>
            <td>0 </td>
            <td>65 </td>
        </tr>
    </table>

그리고 열 8-10 (박쥐, 보울 및 펜)을 삭제하려고합니다. 어디서부터 시작해야하는지 잘 모르겠다. 그래서 어떤 포인터라도 도움이 될 것이다!

수락 된 답변

tr 에 대해 반복하고 각각에서 8th, 9th 및 10th td 노드를 제거해야합니다.

bool first = true;
foreach (HtmlNode row in doc.DocumentNode.SelectNodes("//tr"))
{
    if (first)
    {
        row.RemoveChild(row.SelectSingleNode("th[10]"));
        row.RemoveChild(row.SelectSingleNode("th[9]"));
        row.RemoveChild(row.SelectSingleNode("th[8]"));
        first = false;
    }
    else
    {
        row.RemoveChild(row.SelectSingleNode("td[10]"));
        row.RemoveChild(row.SelectSingleNode("td[9]"));
        row.RemoveChild(row.SelectSingleNode("td[8]"));
    }
}



아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
이 KB는 합법적입니까? 예, 이유를 알아보십시오.
아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
이 KB는 합법적입니까? 예, 이유를 알아보십시오.