Saving A Specific Node As HTML Using HtmlAgilityPack

c# html html-agility-pack

Question

I have a web site that displays a table along with some buttons, navigation tabs, etc. Using HtmlAgilityPack I would like to save only the node containing the table (a div) to a .html file. I have been experimenting with this code:

string dashboard = doc.DocumentNode.SelectSingleNode("//div[@id='Dashboard']").InnerHtml;
doc.LoadHtml(dashboard);
doc.Save(currDir + "\\dashboardOutputFile.html");

But this only preserves the text components of the table and none of its layout/display information. I have tried adding a Doctype declaration as well as enclosing html and body tags to the dashboard string but it has no effect.

Any advice is appreciated.

Regards.

EDIT:

I should be more explicit. I am capturing the html of my page by overriding the Render method:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();

        var doc = new HtmlDocument();
        doc.LoadHtml(renderedContent);

        try
        {
            string dashboard = doc.DocumentNode.SelectSingleNode("//div[@id='Dashboard']").InnerHtml;
            dashboard = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><br><html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body><div><br>" + dashboard + "<br></div></body></html>";
            doc.LoadHtml(dashboard);
            doc.Save(currDir + "\\dashboardOutputFile.html");
        }
        catch (NullReferenceException ex)
        {
            // Do nothing.
        }
        writer.Write(renderedContent);
    }
}

To the best of my knowledge the string "dashboard" should contain a complete HTML page after this. However the resulting .html file displays like this:

INCIDENT MANAGEMENT
Jun '12 F'12
Trend   F'12    2011
(avg)
Severe Incidents (Sev1/2):  2       2   4.16
Severe Avoidable Incidents (Sev1/2):    1       1.3 1.91
Incidents (Sev3):   600     459 460.92
Incidents (Sev4) - No business Impact:  869     782 793
Proactive Tickets Opened:   24      20  14
Proactive Tickets Resolved/Closed:  22      17.8    11
CHANGE MANAGEMENT
Total Planned Changes:  623     564 583.58
Change Success Rate (%):    99.9        99.4    99
Non-Remedial Urgent Changes:    5       11.4    47.08
PROBLEM MANAGEMENT
New PIRs:   1       1.4 2
Closed PIRs:    1       2.3 3
Overdue Action items:   2       3.4 0
COMPLIANCE MEASUREMENTS
    Jun Trend   May
Total Number of Perimeter Devices:  258     242
Perimeter Devices - Non Compliant:  31      7
Total Number of Internal Devices:   6632        6521
Internal Devices - Non Compliant:   160     38
Unauthorized Perimeter Changes: 0       0
Unauthorized Internal Changes   0       0
LEGEND

ISP LINKS
June    May Trend
SOC CPO DRP SOC CPO DRP
39% 35% 74%     38% 35% 73%     
BELL MPLS HEAD ENDS
June    May Trend
SOC CPO SOC CPO
8%  5.5%    7%  10% 7.5%    5%  6%  10% 
ENTERPRISE NETWORK (# of issues called out)
June    May Trend
CORE    FW/DMZ  CORE    FW/DMZ
1   0   1   0   
US & INTL (# of issues called out)
June    May Trend
US  Intl    US  Intl
2   3   2   3   
LINE OF BUSINESS    BELL WAN MPLS
<>  50%-65% >65%    <>  50%-65% >65%    Trend
Retail: 2269    4   0   2271    2   0   
Business Banking:   60  0   0   51  1   0   
Wealth: 121 2   1   111 3   2   
Corporate:  49  2   0   51  1   1   
Remote ATM: 280 0   0   279 0   0   
TOOLS
Version Currency    Vulnerability Status    Health Status
Key Messages:

Only the text of the table cells is being displayed. There is no table layout, no background colours, etc. Even when I copy the file into JSFiddle it displays like this. What am I missing to make this a complete HTML page?

Accepted Answer

There's probably a css file linked into the original html not being read in your downloaded page.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why