【发布时间】:2011-04-29 11:26:12
【问题描述】:
我只是在寻找一种非常简单的方法来清理一些 HTML(可能使用嵌入式 JavaScript 代码)。我试过twodifferentHTML 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();
}
【问题讨论】:
-
所以你只是想重新格式化你的源代码?你可以使用任何 web-ide 或 Notepad++。
-
@Nick:我意识到这一点,但我不尝试重新格式化我已经拥有的 HTML 文件.. 我正在尝试重新格式化我正在生成的 HTML C# 应用程序...
-
检查 HtmlTextWriter,我更新了答案
-
只是为了自我推销,my version 从 2007 年回到代码项目。仍在商业项目中使用它。