我可以使用Html Agility Pack使输出看起来很好地缩进,不必要的空白区域被剥离了吗?
我只是在寻找一种非常简单的方法来清理一些HTML(可能带有嵌入式JavaScript代码).我尝试了两个 不同的 HTML Tidy .NET端口,两者都抛出异常......
对不起,"干净"是指"缩进".HTML根本没有格式错误.这是严格的XHTML.
我终于得到了一些使用SGML的东西,但这是一个非常荒谬的代码块,可以缩进一些HTML.
private static string FormatHtml(string input)
{
var sgml = new SgmlReader {DocType = "HTML", InputStream = new StringReader(input)};
using (var sw = new StringWriter())
using (var xw = new XmlTextWriter(sw) { Indentation = 2, Formatting = Formatting.Indented })
{
sgml.Read();
while (!sgml.EOF)
xw.WriteNode(sgml, true);
}
return sw.ToString();
}
Run Code Online (Sandbox Code Playgroud)