我试图用HtmlAgilityPack从头创建一个XHTMl文件。继Add a doctype to HTML via HTML Agility pack提出的建议,我尝试将文档类型添加到它:使用HtmlAgilityPack的doctype的问题
private static HtmlDocument createEmptyDoc()
{
HtmlDocument titlePage = new HtmlDocument();
titlePage.OptionOutputAsXml = true;
titlePage.OptionCheckSyntax = true;
titlePage.AddDoctype();
var html = titlePage.CreateElement("html");
titlePage.DocumentNode.AppendChild(html);
return titlePage;
}
public static class HtmlDocumentExtensions
{
public static void AddDoctype(this HtmlDocument doc)
{
var doctype = doc.DocumentNode.PrependChild(doc.CreateComment("<!doctype html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"));
}
}
然而,当我这个文件写入一个文件,它看起来像这样:
<?xml version="1.0" encoding="iso-8859-1"?>
<!--type html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.d-->
<html />
的DOCTYPE真正得到作为评论对待,一些字符被短划线代替。我该如何解决这个问题,并将doctype原样写入文件?
编辑:添加自定义扩展到HTMLDocument的
我不是在Windows上,所以我不能对此进行测试,但似乎你不按照这些答案的建议。另外,'OptionOutputAsXml = true'可能会导致部分问题。 – GolfWolf
检查我的答案,并告诉我是否帮助你。 – mybirthname
@ w0lf你能告诉我我没有听从建议吗?我需要'OptionOutputAsXml = true',因为我需要一个XHTML文档。 – Thaoden