使用HtmlAgilityPack將特定節點保存為HTML

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頁面?

一般承認的答案

可能有一個鏈接到原始html的css文件未在您下載的頁面中讀取。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因