C# version of HTML Tidy?

老子叫甜甜 提交于 2019-11-28 07:45:35
wonea

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.

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

What is exactly the Html Agility Pack (HAP)?

This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).

Html Agility Pack now supports Linq to Objects (via a LINQ to Xml Like interface). Check out the new beta to play with this feature

Sample applications:

  • Page fixing or generation. You can fix a page the way you want, modify the DOM, add nodes, copy nodes, well... you name it.

  • Web scanners. You can easily get to img/src or a/hrefs with a bunch XPATH queries.

  • Web scrapers. You can easily scrap any existing web page into an RSS feed for example, with just an XSLT file serving as the binding. An example of this is provided.


Also you can try this implementation: A managed wrapper for the HTML Tidy library

AngleSharp 100% c#

    var parser = new HtmlParser();

    var document = parser.Parse("<html><head></head><body><i></i></body></html>");

    var sw = new StringWriter();
    document.ToHtml(sw, new PrettyMarkupFormatter());

    var HTML_prettified = sw.ToString());

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.

You can use HtmlAgilityPack (add this package from nuget).

Code sample:

string html = "<div><p>line 1<br>line 2</p><span></div>";
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(description);
var fixedHtml = htmlDoc.DocumentNode.OuterHtml;

Output:

<div><p>line 1<br />line 2</p><span></span></div>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!