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合法吗? 是的,了解原因