HtmlAgilityPack HtmlDocument.Save() Behaving Differently Depending On Save-To Directory

c# html-agility-pack

Question

I need to save the output from a web page as a .html file so that I can convert it into an image later. Here is the code I am using to strip the unwanted components and leave just the table:

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

When I call:

doc.Save(currDir + "\\dashboardTableOutput.html");

I end up with a .html file that displays this way:

enter image description here

When I call

doc.Save(currDir + "\\" + reportDir + "\\dashboardTableOutput.html");

I end up with a .html file that displays this way:

enter image description here

with all of the layout and graphical information missing. I can't think of any reason why changing the directory I save to would have this affect.

Any advice is appreciated.

Regards.

Accepted Answer

My guess is that you already have a .CSS file somewhere (maybe in currDir + "\\" + reportDir?) that is being referenced by the .html file- correctly, in the one case, and not correctly, in the other.



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