How to Import HTML doc Table to Datatable?

Mar 26, 2013 at 9:52 AM
Hi,

I have writen the code like this, but it did not work - in my node list it is coming as null..
strResult = "<html><body>" + strResult + "</body></html>";
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(strResult);
        var nodes = doc.DocumentNode.SelectNodes("table");
        var table = new DataTable("MyTable");
        var headers = nodes[0].Elements("th").Select(th => th.InnerText.Trim());

        foreach (var header in headers)
        {
            table.Columns.Add(header);
        }

and my html will be
<html><body><table><tbody>
<tr style="TOP: 0px" id="TableHeaderTopRow" class="TableHeaderTopRow">
<th>
<div class="ListHeaderTopCell"><span>Order Code</span> </div></th>
<th>
<div class="ListHeaderTopCell"><span>Order Code Description</span> </div></th>
<th>
<div class="ListHeaderTopCell Centered"><span>Status</span> </div></th>
<th>
<div class="ListHeaderTopCell Centered"><span>Maintenance</span> </div></th>
<th colspan="2">
<div class="ListHeaderTopCell"><span>Chassis</span> </div></th>
<th>
<div class="ListHeaderTopCell Centered"><span>Chassis Status</span> </div></th>
<th colspan="2">
<div class="ListHeaderTopCell"><span>Brand</span> </div></th>
<th colspan="2">
<div class="ListHeaderTopCell"><span>System Family</span> </div></th>
<th>
<div class="ListHeaderTopCell"><span>Owner</span> </div></th>
<th>
<div class="ListHeaderTopCell"><span>OC Type</span> </div></th>
<th>
<div class="ListHeaderTopCell Centered"><span>Preview</span> </div></th></tr>
<tr style="TOP: 0px" id="TableHeaderBottomRow" class="TableHeaderBottomRow">
<th>
<div class="ListHeaderBottomCell Left"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('ocId;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('ocDesc;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left Centered"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('ocStatus;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('ocMaintenance;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('chassisId;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('chassisName;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left Centered"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('chassisStatus;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Descending" onclick="sortTable('brandId;;desc');" src="../../image/SortArrowUp.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('brandName;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('familyId;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('familyName;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('owner;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left"><span></span><img style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Sort Ascending" onclick="sortTable('ocType;;asc');" src="../../image/SortArrowDown.jpg"> </div></th>
<th>
<div class="ListHeaderBottomCell Left"><span>&nbsp;</span> </div></th></tr>
<tr id="Record--dellstar_1199_3" class="WhiteRow">
<td class="Normal EditConfigLink"><a onclick="stopValidating()" href="EditOrderCode.aspx?view=option&orderCode=dellstar_1199_3">dellstar_1199_3</a></td>
<td class="Normal">XPS 8500_i7/12/2</td>
<td id="Status--dellstar_1199_3" class="Normal Centered">I</td>
<td class="Normal">Single<input id="hdnShared_dellstar_1199_3" value="0" type="hidden" name="hdnShared_dellstar_1199_3"></td>
<td class="Normal">1199</td>
<td class="Normal">XPS 8500</td>
<td class="Normal Centered">A</td>
<td class="Normal">1</td>
<td class="Normal">Dell XPS Desktops</td>
<td class="Normal">10060</td>
<td class="Normal">XPS 8500</td>
<td id="Owner--dellstar_1199_3" class="Normal">Panchori_Tarun</td>
<td id="OcType--dellstar_1199_3" class="Normal">DellStar</td>
<td class="Normal Centered"><input id="previewdellstar_1199_3" value="dellstar_1199_3" type="radio" name="preview"></td></tr>
</tbody></table></body></html>