C# HtmlAgilityPack将

C# HtmlAgilityPack将,c#,html-agility-pack,C#,Html Agility Pack,我有一些通过文本区获得的输入,我将这些输入转换成html文档,然后解析成PDF文档 当我的用户输入小于号时 我在htmldocument上尝试了所有选项,但没有一个选项允许我指定解析器不应严格。另一方面,我可能会接受它剥离你的内容是明显错误的。这不是严格的要求,而是假装一段文本是有效的HTML。事实上,您得到的结果正是因为解析器不严格 当您需要将纯文本插入HTML时,首先需要对其进行编码,以便将所有不同的HTML控制字符正确地转换为HTML—例如,您的内容显然是错误的。这不是严格的要求,而是假装

我有一些通过文本区获得的输入,我将这些输入转换成html文档,然后解析成PDF文档

当我的用户输入小于号时


我在htmldocument上尝试了所有选项,但没有一个选项允许我指定解析器不应严格。另一方面,我可能会接受它剥离你的内容是明显错误的。这不是严格的要求,而是假装一段文本是有效的HTML。事实上,您得到的结果正是因为解析器不严格


当您需要将纯文本插入HTML时,首先需要对其进行编码,以便将所有不同的HTML控制字符正确地转换为HTML—例如,您的内容显然是错误的。这不是严格的要求,而是假装一段文本是有效的HTML。事实上,您得到的结果正是因为解析器不严格

当您需要将纯文本插入HTML时,首先需要对其进行编码,以便将所有不同的HTML控制字符正确地转换为HTML-例如,您可以使用它,即使没有对System.Web.dll的引用也可以使用它

即使没有对System.Web.dll的引用,也可以使用它,System.Web.dll还具有


HTML转义您的内容。如果它包含标记字符,它肯定会中断。我想这不仅仅会中断HtmlAgilityPack。试着在浏览器中查看它。HTML转义您的内容。如果它包含标记字符,它肯定会中断。我想这不仅仅会中断HtmlAgilityPack。试着在浏览器中查看它,你是对的。我的问题是,我已经尝试过对html进行编码,而我创建PDF的引擎没有对它们进行解码,但我当然可以自己解码,然后它就工作了。在另一个节点上,我无法使用InnerText,因为它是只读的,从上面的输入创建新节点不起作用。你是对的。我的问题是,我已经尝试过对html进行编码,而我创建PDF的引擎没有对它们进行解码,但我当然可以自己解码,然后它就工作了。在另一个节点上,我无法使用InnerText,因为它是只读的,并且从上面的输入创建新节点不起作用。
htmlDocument.OptionOutputOptimizeAttributeValues = true;
void Main()
{
    var input = @"Within this Character Data block I can use double dashes as much as I want (along with <, &, ', and ') *and * % MyParamEntity; will be expanded to the text 'Has been expanded'...however, I can't use the CEND sequence(if I need to use it I must escape one of the brackets or the greater-than sign).";

    var htmlDoc = WrapContentInHtml(input);

    htmlDoc.DocumentNode.OuterHtml.ToString().Dump();
}

private HtmlDocument WrapContentInHtml(string content)
{
    var htmlBuilder = new StringBuilder();
    htmlBuilder.AppendLine("<!DOCTYPE html>");
    htmlBuilder.AppendLine("<html>");
    htmlBuilder.AppendLine("<head>");
    htmlBuilder.AppendLine("<title></title>");
    htmlBuilder.AppendLine("</head>");
    htmlBuilder.AppendLine("<body><div id='sagsfremstillingContainer'>");
    htmlBuilder.AppendLine(content); 
    htmlBuilder.AppendLine("</div></body></html>");

    var htmlDocument = new HtmlDocument();
    htmlDocument.OptionOutputOptimizeAttributeValues = true;
    var htmlDoc = htmlBuilder.ToString();

    htmlDocument.LoadHtml(htmlDoc);

    return htmlDocument;
}
var input = @"Within this Character Data block I can use double dashes as much as I want (along with <, &, ', and ') *and * % MyParamEntity; will be expanded to the text 'Has been expanded'...however, I can't use the CEND sequence(if I need to use it I must escape one of the brackets or the greater-than sign).";
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(System.Net.WebUtility.HtmlEncode(input));
Debug.Assert(!htmlDocument.ParseErrors.Any());
Within this Character Data block I can use double dashes as much as I want (along with &lt;, &amp;, &#39;, and &#39;) *and * % MyParamEntity; will be expanded to the text &#39;Has been expanded&#39;...however, I can&#39;t use the CEND sequence(if I need to use it I must escape one of the brackets or the greater-than sign).