HtmlAgilityPack HtmlDocument.Save()根據保存目錄的不同行為

c# html-agility-pack

我需要將網頁的輸出保存為.html文件,以便稍後將其轉換為圖像。這是我用來剝離不需要的組件而只留下表格的代碼:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();
        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");
        output = output.Replace(@"<a href=""http://w3.td.com/td/intranet/tdweb""><img src=""Trends/TDBFGLogo5.gif"" alt=""TD Home"" title=""TD Home"" height=""40px"" width=""40px"" style=""float: left; border: none"" /></a>", "");
        output = output.Replace(@"<hr style=""background-color: #8bb093; height: 30px; width: 100%; float: left"" />", "");
        output = output.Replace(@"<h1>Network Operations Dashboard</h1>", "");

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(output);

        HtmlNode node = doc.DocumentNode.SelectSingleNode("//select[@name='Archives']");
        node.Remove();
        node = doc.DocumentNode.SelectSingleNode("//ul[@id='mainNav']");
        node.Remove();
        string fileName = currDir + "\\dashboardTableOutput.html";
        doc.Save(fileName);

        writer.Write(renderedContent);
    }
}

我打電話的時候:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();
        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");
        output = output.Replace(@"<a href=""http://w3.td.com/td/intranet/tdweb""><img src=""Trends/TDBFGLogo5.gif"" alt=""TD Home"" title=""TD Home"" height=""40px"" width=""40px"" style=""float: left; border: none"" /></a>", "");
        output = output.Replace(@"<hr style=""background-color: #8bb093; height: 30px; width: 100%; float: left"" />", "");
        output = output.Replace(@"<h1>Network Operations Dashboard</h1>", "");

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(output);

        HtmlNode node = doc.DocumentNode.SelectSingleNode("//select[@name='Archives']");
        node.Remove();
        node = doc.DocumentNode.SelectSingleNode("//ul[@id='mainNav']");
        node.Remove();
        string fileName = currDir + "\\dashboardTableOutput.html";
        doc.Save(fileName);

        writer.Write(renderedContent);
    }
}

我最終得到一個顯示這種方式的.html文件:

在此處輸入圖像描述

我打電話的時候

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();
        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");
        output = output.Replace(@"<a href=""http://w3.td.com/td/intranet/tdweb""><img src=""Trends/TDBFGLogo5.gif"" alt=""TD Home"" title=""TD Home"" height=""40px"" width=""40px"" style=""float: left; border: none"" /></a>", "");
        output = output.Replace(@"<hr style=""background-color: #8bb093; height: 30px; width: 100%; float: left"" />", "");
        output = output.Replace(@"<h1>Network Operations Dashboard</h1>", "");

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(output);

        HtmlNode node = doc.DocumentNode.SelectSingleNode("//select[@name='Archives']");
        node.Remove();
        node = doc.DocumentNode.SelectSingleNode("//ul[@id='mainNav']");
        node.Remove();
        string fileName = currDir + "\\dashboardTableOutput.html";
        doc.Save(fileName);

        writer.Write(renderedContent);
    }
}

我最終得到一個顯示這種方式的.html文件:

在此處輸入圖像描述

缺少所有佈局和圖形信息。我想不出有什麼理由為什麼更改我保存的目錄會產生這種影響。

任何建議表示讚賞。

問候。

一般承認的答案

我的猜測是你已經有一個.CSS文件(可能在currDir + "\\" + reportDir ?中)正在被.html文件引用 - 在一種情況下正確引用,而在另一種情況下則不正確。




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