admin管理员组

文章数量:1431426

I'm using the Html Agility Pack to output some javascript in the head of my document. But after saving the document to the file system I recognized that the javascript source has been modified. I guess this happens because HAP is trying to validate my script. Is it possible to prevent this? As you can see below I already tried setting different options.

My code using HAP:

var htmlDoc = new HtmlDocument();
htmlDoc.OptionCheckSyntax = false;
htmlDoc.OptionAutoCloseOnEnd = false;
htmlDoc.OptionFixNestedTags = false;
htmlDoc.LoadHtml(htmlContent);

HtmlNode headNode = htmlDoc.DocumentNode.SelectSingleNode("//head");
headNode.AddScriptNode(htmlDoc, "../../Scripts/jquery-1.7.1.min.js");

Extension Method for adding the script tag

public static void AddScriptNode(this HtmlNode headNode, HtmlDocument htmlDoc, string filePath)
{
    string content = "";

    using (StreamReader rdr = File.OpenText(filePath))
    {
        content = rdr.ReadToEnd();
    }
    if(headNode != null)
    {
        HtmlNode scripts = htmlDoc.CreateElement("script");
        scripts.Attributes.Add("type", "text/javascript");
        scripts.InnerHtml = "\n" + content + "\n";
        headNode.AppendChild(scripts);
    }
}

I'm using the Html Agility Pack to output some javascript in the head of my document. But after saving the document to the file system I recognized that the javascript source has been modified. I guess this happens because HAP is trying to validate my script. Is it possible to prevent this? As you can see below I already tried setting different options.

My code using HAP:

var htmlDoc = new HtmlDocument();
htmlDoc.OptionCheckSyntax = false;
htmlDoc.OptionAutoCloseOnEnd = false;
htmlDoc.OptionFixNestedTags = false;
htmlDoc.LoadHtml(htmlContent);

HtmlNode headNode = htmlDoc.DocumentNode.SelectSingleNode("//head");
headNode.AddScriptNode(htmlDoc, "../../Scripts/jquery-1.7.1.min.js");

Extension Method for adding the script tag

public static void AddScriptNode(this HtmlNode headNode, HtmlDocument htmlDoc, string filePath)
{
    string content = "";

    using (StreamReader rdr = File.OpenText(filePath))
    {
        content = rdr.ReadToEnd();
    }
    if(headNode != null)
    {
        HtmlNode scripts = htmlDoc.CreateElement("script");
        scripts.Attributes.Add("type", "text/javascript");
        scripts.InnerHtml = "\n" + content + "\n";
        headNode.AppendChild(scripts);
    }
}
Share Improve this question asked Feb 27, 2012 at 13:42 KuepperKuepper 1,00415 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

My assumption: when using scripts.InnerHtml AgilityPack tries to parse the content as HTML. So if there are tags there they will be converted to HTML nodes.

To avoid this you should set the content of the <script> as text. Unfortunately, HtmlNode.InnerText property is a read-only but there is a workaround for this. You could just add a text(a ment node will be preferrable) node to your <script> node:

if(headNode != null)
{
    HtmlNode scripts = htmlDoc.CreateElement("script");
    scripts.Attributes.Add("type", "text/javascript");
    scripts.AppendChild(htmlDoc.CreateComment("\n" + content + "\n"));
    headNode.AppendChild(scripts);
}

Here the body of your script will be added as a ment node (<!-- and --> will be added).

本文标签: Html Agility Pack messing with my javascriptStack Overflow