Saving A Specific Node As HTML Using HtmlAgilityPack

c# html html-agility-pack

Question

On my website, a table appears among buttons, menu tabs, and other elements. I want to save just the node (a div) that contains the table to a.html file using HTML Agility Pack. I've been playing around with this code:

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

However, only the text elements of the table are preserved; the layout and display data are not preserved. I've tried adding a Doctype declaration and wrapping the dashboard string with html and body tags, but it makes no difference.

I would appreciate any input.

Regards.

EDIT:

I ought to be clearer. By overriding the Render method, I am collecting the HTML of my page:

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);
    }
}

Should the best of my understanding, the string "dashboard" ought to follow this with a full HTML page. But here is how the resulting.html file looks:

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:

The only thing seen is the text of the table cells. There are no backdrop colors, table layouts, etc. This is how it appears even when I transfer the file into JSFiddle. What do I need to add for this to be a full HTML page?

1
0
8/23/2012 7:30:48 PM

Accepted Answer

In your downloaded website, a css file that was linked into the original html is possibly not being read.

0
8/23/2012 7:37:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow