HTML Tidy 的 C# 版本?
我只是在寻找一种非常简单的方法来清理一些 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();
}
I am just looking for a really easy way to clean up some HTML (possibly with embedded JavaScript code). I tried two different HTML Tidy .NET ports and both are throwing exceptions...
Sorry, by "clean" I mean "indent". The HTML is not malformed, at all. It's XHTML strict.
I finally got something working with SGML, but this is seriously the most ridiculous chunk of code ever to indent some 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
AngleSharp 100% c#
由塞巴斯蒂安编辑:
AngleSharp 100% c#
edit by sebastian :
HTML Tidy 的最新 C# 包装器是由 Mark Beaton 完成的,它看起来比您引用的链接 (2003) 更新得多。另外值得注意的是,Mark 还提供了可执行文件供参考,而不是从官方网站获取它们。这应该能够很好地组织和验证您的 HTML。
The latest C# wrapper for HTML Tidy was done by Mark Beaton, which seems rather more up-to-date than the links you've referenced (2003). Also worth of note is that Mark provides executables for referencing as well, rather than pulling them from the official site. That should do the trick of nicely organising and validating your HTML.
我使用 SGML Reader 来将 HTML 转换为 XHTML 过去。可能值得研究一下......
我在使用它时从未遇到过任何问题。
I've used SGML Reader to convert HTML to XHTML in the past. Might be worth looking into...
I never had any problems with it when I was using it.
更新:
检查HtmlTextWriter 或 XhtmlTextWriter,用法:使用 HtmlTextWriter 格式化 Html 输出,也许 通过HtmlTextWriter构建HTML会更好吗?
另请检查: LINQ & Lambda,第 3 部分:Html 敏捷包到 LINQ to XML 转换器
http://www.manoli.net /csharpformat/,这里源代码以防您错过。
也许你想自己做?这个项目可能会有所帮助:Html Agility Pack
您也可以尝试以下实现:A Managed Wrapper for the HTML Tidy library
UPDATE:
Check HtmlTextWriter or XhtmlTextWriter, usage: Formatting Html Output with HtmlTextWriter, maybe HTML construction via HtmlTextWriter will be better?
Also check : LINQ & Lambda, Part 3: Html Agility Pack to LINQ to XML Converter
http://www.manoli.net/csharpformat/, here source code in case you miss it.
Maybe you want to do it yourself? This project can be helpful: Html Agility Pack
Also you can try this implementation: A managed wrapper for the HTML Tidy library
您可以使用HtmlAgilityPack(从nuget添加此包)。
代码示例:
输出:
You can use HtmlAgilityPack (add this package from nuget).
Code sample:
Output:
Beautifier提供html我用的是html-beautify。
例如
Beautifier provides html I used html-beautify.
for example