使用HtmlAgilityPack将特定节点保存为HTML

时间:2012-08-22 21:23:32

标签: c# html html-agility-pack

我有一个网站,显示一个表格以及一些按钮,导航标签等。使用HtmlAgilityPack我想只保存包含表格(div)的节点到.html文件。我一直在试验这段代码:

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

但是这只保留了表的文本组件,而没有保留其布局/显示信息。我已经尝试添加Doctype声明以及将html和body标签包含在仪表板字符串中但它没有效果。

感谢任何建议。

问候。

编辑:

我应该更明确。我通过覆盖Render方法来捕获页面的html:

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

据我所知,字符串“dashboard”应该包含一个完整的HTML页面。但是,生成的.html文件显示如下:

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:

仅显示表格单元格的文本。没有表格布局,没有背景颜色等。即使我将文件复制到JSFiddle,它也会显示如下。我错过了什么使这个完整的HTML页面?

1 个答案:

答案 0 :(得分:0)

可能有一个链接到原始html中的css文件未在您下载的页面中读取。