HTMLAGILITYPACK을 사용하여 html 구문 분석 및 datatable C #

c# html html-agility-pack

문제

나는 다음과 같은 HTML을 가지고있다 :

<body class="style_0">
        <div>
            <div class="style_1">Pending Test List</div>
            <table style=" width: 100%;" id="AUTOGENBOOKMARK_4365445353431356880">
                <col>
                <col>
                <tbody>
                    <tr>
                        <td style="vertical-align: baseline;">
                            <div class="style_4">Pending Test List</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_5">SOME AGENCY Laboratories, Inc.</div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <table class="style_6" style=" width: 4.531in;" id="AUTOGENBOOKMARK_5083738604442918131">
                <col style=" width: 1in;">
                <col class="style_7" style=" width: 0.75in;">
                <col class="style_8" style=" width: 0.6in;">
                <col style=" width: 0.75in;">
                <col style=" width: 2.375in;">
                <tbody>
                    <tr class="style_9" style=" height: 0.5in;">
                        <td style="vertical-align: middle;">
                            <div class="style_10">Report Range:</div>
                        </td>
                        <td style="vertical-align: middle;">
                            <div class="style_11">01/01/2012</div>
                        </td>
                        <td style="vertical-align: middle;">
                            <div class="style_12">through</div>
                        </td>
                        <td style="vertical-align: middle;">
                            <div class="style_13">01/31/2012</div>
                        </td>
                        <td style="vertical-align: middle;">
                            <div class="style_14">(by Date Entered)</div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <table class="style_15" style=" width: 100%;" id="AUTOGENBOOKMARK_7602283385844673591" iid="/526

(QuRs78576248:0)">
                <col style=" width: 0.75in;">
                <col style=" width: 1.25in;">
                <col style=" width: 1in;">
                <col style=" width: 1.5in;">
                <col style=" width: 1.5in;">
                <col style=" width: 1.5in;">
                <col>
                <thead>
                    <tr>
                        <td colspan="4" style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                    </tr>
                    <tr>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Entered</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Spec. ID</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Batch/Pos.</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Test</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Client ID</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Client Name</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Agency</div>
                        </td>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">1/30/12</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_19">ZZ324sdf</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">51446 / 75</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">HOLD_DE</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">234234</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">smith, john</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">PPPM-6P - SOME AGENCY</div>
                        </td>
                    </tr>
                    <tr>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">1/31/12</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_19">SFD3434</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">51668 / 17</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">HOLD_DE</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">FOY, EL</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">FOY, ALEX</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">someagency &amp; Associates LLC</div>
                        </td>
                    </tr>
                    <tr>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">1/31/12</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_19">SFD3434</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">51668 / 25</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">HOLD_DE</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">JAMISON, PA</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">JAMISON, ROY</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">someagency &amp; Associates LLC</div>
                        </td>
                    </tr>
                    <tr>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">1/31/12</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_19">SFD3434</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">51669 / 34</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">HOLD_DE</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">NEWMAN, SO</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">NEWMAN, ALEX</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">someagency &amp; Associates LLC</div>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="2" style="vertical-align: baseline;">
                            <div class="style_21">Total Tests:</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_22">4</div>
                        </td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                    </tr>
                </tfoot>
            </table>
            <table style=" width: 100%;" id="AUTOGENBOOKMARK_8507236727661888074">
                <col>
                <col>
                <col>
                <tbody>
                    <tr>
                        <td style="vertical-align: baseline;">
                            <div class="style_2">
                                <br>Feb 13, 2012 9:37 AM</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_3">
                                <br>
                                <div style="text-align:center;">Page 1</div>
                            </div>
                        </td>
                        <td style="vertical-align: baseline;"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>

렌더링 될 때 다음과 같이 보입니다.

여기에 이미지 설명을 입력하십시오.

여기에 내가 파싱하고 싶은 데이터가 있습니다.

2012 년 1 월 30 일 ZZ324sdf 51446 / 75 HOLD_DE 234234 smith, john PPPM-6P - 일부 대리점 SFD3434 51668 / 17 HOLD_DE FOY, EL FOY, ALEX 일부 및 제휴사 LLC 1/31/12 SFD3434 51668 / 25 HOLD_DE JAMISON, PA JAMISON, ROY 일부 및 제휴사 LLC 1/31/12 SFD3434 51669 / 34 HOLD_DE NEWMAN, SO NEWMAN, ALEX someagency & Associates LLC

지금까지 나는 시도했다.

<body class="style_0">
        <div>
            <div class="style_1">Pending Test List</div>
            <table style=" width: 100%;" id="AUTOGENBOOKMARK_4365445353431356880">
                <col>
                <col>
                <tbody>
                    <tr>
                        <td style="vertical-align: baseline;">
                            <div class="style_4">Pending Test List</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_5">SOME AGENCY Laboratories, Inc.</div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <table class="style_6" style=" width: 4.531in;" id="AUTOGENBOOKMARK_5083738604442918131">
                <col style=" width: 1in;">
                <col class="style_7" style=" width: 0.75in;">
                <col class="style_8" style=" width: 0.6in;">
                <col style=" width: 0.75in;">
                <col style=" width: 2.375in;">
                <tbody>
                    <tr class="style_9" style=" height: 0.5in;">
                        <td style="vertical-align: middle;">
                            <div class="style_10">Report Range:</div>
                        </td>
                        <td style="vertical-align: middle;">
                            <div class="style_11">01/01/2012</div>
                        </td>
                        <td style="vertical-align: middle;">
                            <div class="style_12">through</div>
                        </td>
                        <td style="vertical-align: middle;">
                            <div class="style_13">01/31/2012</div>
                        </td>
                        <td style="vertical-align: middle;">
                            <div class="style_14">(by Date Entered)</div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <table class="style_15" style=" width: 100%;" id="AUTOGENBOOKMARK_7602283385844673591" iid="/526

(QuRs78576248:0)">
                <col style=" width: 0.75in;">
                <col style=" width: 1.25in;">
                <col style=" width: 1in;">
                <col style=" width: 1.5in;">
                <col style=" width: 1.5in;">
                <col style=" width: 1.5in;">
                <col>
                <thead>
                    <tr>
                        <td colspan="4" style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                    </tr>
                    <tr>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Entered</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Spec. ID</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Batch/Pos.</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Test</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Client ID</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Client Name</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_16">Agency</div>
                        </td>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">1/30/12</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_19">ZZ324sdf</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">51446 / 75</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">HOLD_DE</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">234234</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">smith, john</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">PPPM-6P - SOME AGENCY</div>
                        </td>
                    </tr>
                    <tr>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">1/31/12</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_19">SFD3434</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">51668 / 17</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">HOLD_DE</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">FOY, EL</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">FOY, ALEX</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">someagency &amp; Associates LLC</div>
                        </td>
                    </tr>
                    <tr>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">1/31/12</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_19">SFD3434</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">51668 / 25</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">HOLD_DE</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">JAMISON, PA</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">JAMISON, ROY</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">someagency &amp; Associates LLC</div>
                        </td>
                    </tr>
                    <tr>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">1/31/12</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_19">SFD3434</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_18">51669 / 34</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">HOLD_DE</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">NEWMAN, SO</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">NEWMAN, ALEX</div>
                        </td>
                        <td class="style_17" style="vertical-align: baseline;">
                            <div class="style_20">someagency &amp; Associates LLC</div>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="2" style="vertical-align: baseline;">
                            <div class="style_21">Total Tests:</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_22">4</div>
                        </td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                        <td style="vertical-align: baseline;"></td>
                    </tr>
                </tfoot>
            </table>
            <table style=" width: 100%;" id="AUTOGENBOOKMARK_8507236727661888074">
                <col>
                <col>
                <col>
                <tbody>
                    <tr>
                        <td style="vertical-align: baseline;">
                            <div class="style_2">
                                <br>Feb 13, 2012 9:37 AM</div>
                        </td>
                        <td style="vertical-align: baseline;">
                            <div class="style_3">
                                <br>
                                <div style="text-align:center;">Page 1</div>
                            </div>
                        </td>
                        <td style="vertical-align: baseline;"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>

하지만 이것은 AHREF 만 추출한다는 것을 알고 테이블 요소를 추출하려고합니다.

어떻게해야합니까? 도와 주셔서 정말 감사합니다.

수락 된 답변

약간 씩 다르게 생각하십시오 - 모든 앵커 (href 포함)를 원하시는 대신 style_15 클래스의 테이블 본문에있는 모든 행을 style_15 (이 id는 매우 style_15 생성됩니다). 그러면 모든 행에 대해 모든 셀을 원할 것입니다.

foreach (var row in htmlSnippet.DocumentNode.SelectNodes("//table[@class = 'style_15']/tbody/tr"))
{
    foreach (var cell in row.SelectNodes("td"))
    {
        // Do something
    }
}



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