We are working with HtmlAgilityPack ... trying to add two nodes to a document
We use the following :
HtmlNode styleNode = HtmlNode.CreateNode("<style>"+ style + "</style>");
HtmlNode jsNode= HtmlNode.CreateNode("<script>"+ js + "</script>");
page_body.AppendChild(styleNode);
page_body.AppendChild(jsNode);
The result we get are odd, if we add both nodes, then the style is being appended to the final saved document, and the script is not.
but when we mark out the style node :
// HtmlNode styleNode = HtmlNode.CreateNode("<style>"+ style + "</style>");
HtmlNode jsNode= HtmlNode.CreateNode("<script>"+ js + "</script>");
// page_body.AppendChild(styleNode);
page_body.AppendChild(jsNode);
Then the script node is appended and evaluated just fine...
What are we doing wrong? why can't these two nodes just get along?
...
The full code
var pageDoc = new HtmlDocument();
var title = page.GetAttributeValue("title", ""); //String.IsNullOrEmpty(title)
var page_head = page.SelectSingleNode("//page-head");
var page_body = page.SelectSingleNode("//page-body");
var page_foot = page.SelectSingleNode("//page-footer");
if (page_head == null) page_head = default_head.Clone();
if (page_foot == null) page_foot = default_foot.Clone();
// fetch and add data
HtmlNode script_node = HtmlNode.CreateNode("<script>" + js_handler + "</script>");
HtmlNode style_node = HtmlNode.CreateNode("<style>" + general_style + style.InnerHtml + "</style>");
page_body.AppendChild(script_node);
page_body.AppendChild(style_node);
HtmlNodeCollection childNodes = page_body.ChildNodes;
foreach (var node in childNodes)
{
if (node.NodeType == HtmlNodeType.Element)
{
Console.WriteLine(node.Name);
}
}
string html_file = EXPORT_HTML_PATH + token + "_" + page_num + ".html";
string pdf_file = EXPORT_PDF_PATH + token + "_" + page_num + ".pdf";
FileStream sw = new FileStream(html_file, FileMode.Create); // specific page count
pageDoc.LoadHtml(page.OuterHtml);
pageDoc.Save(sw);
sw.Close();
Your approach seems okay, you need to share full method code to find the bug. Here is a fiddle where you can see its okay. May be your 'style' or 'script' variable got something crazy which cause the problem.