9

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();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • So you just want to reformat your source code? You can use any web-ide or Notepad++ for that. – Nick Martyshchenko Oct 23 '10 at 03:59
  • @Nick: I realize that, but I'm *not* trying reformat HTML files I already have.. I'm trying to reformat HTML that I'm generating in a C# app... – mpen Oct 23 '10 at 04:14
  • check HtmlTextWriter, I updated my answer – Nick Martyshchenko Oct 23 '10 at 04:28
  • 2
    Just to self-promote, [my version](http://www.codeproject.com/Articles/17124/A-managed-wrapper-for-the-HTML-Tidy-library) back from 2007 ist over at The Code Project. Still using it in commercial projects. – Uwe Keim Sep 06 '13 at 15:43

6 Answers6

18

AngleSharp 100% c#

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

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

    var HTML_prettified = sw.ToString();

edit by sebastian :

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

 //new parse method (for AngleSharp 0.16.1): 
 var document = await parser.ParseDocumentAsync(Code); 
 
bh_earth0
  • 2,537
  • 22
  • 24
  • Seems like a great project but it does not clean HTML. Would be nice if they offered some option to actually clean html, or better convert to XHTML, but it does not seem to target those scenarios. – MikeJ May 21 '20 at 13:26
  • This should be the accepted answer. It's 100% C# and does what the OP asked for in 5 lines of code. – dkm Apr 09 '21 at 18:38
  • 1
    Updated version (Nuget Package AngleSharp 0.16.1): ``` var parser = new HtmlParser(); var document = await parser.ParseDocumentAsync(Code); var sw = new StringWriter(); document.ToHtml(sw, new PrettyMarkupFormatter()); var HTML_prettified = sw.ToString(); ``` – Sebastian Feb 16 '22 at 12:08
9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wonea
  • 4,783
  • 17
  • 86
  • 139
  • 2
    The builds are just for tidylib, not the C# wrapper. You'll need to build TidyManaged from source as well. I'm running a 64-bit machine, but only the 32-bit tidylib dll works, for whatever reason. I had to put it in c:/windows/system. Also, the example Beaton provides won't indent your HTML -- the only thing I wanted -- you need to add `doc.IndentBlockElements = AutoBool.Auto`... little tricky to figure out. – mpen Jan 11 '11 at 20:09
  • Agreed, I've came rather un-stuck after moving to x64 and tidylib is throwing an exception "BadImageFormatException occured - An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)". Posted a bug on TidyManaged https://github.com/markbeaton/TidyManaged/issues/3 – wonea Mar 22 '11 at 09:57
  • I've managed to get this working on Windows 7 64 bit by changing the project to x86 in Configuration Manager on both the TidyManaged project and my project that references it and using the 32 bit version of libtidy.dll. – ChrisR Apr 18 '11 at 18:29
  • I've just tried my 64-bit build of libtidy.dll under 64-bit Windows 7, and as long as the TidyManaged wrapper code is built using "Any CPU" and your referencing project is also "Any CPU", things work fine. If running under ASP.NET, you'll need to make sure your app pool is running in 64-bit mode as well ("Enable 32-bit applications" should be false). Also, you shouldn't need to drop libtidy.dll into your system directory - just putting it into your app's bin folder should be enough. – Mark Beaton Jun 10 '11 at 01:34
  • 1
    Also, I've just uploaded a release "Any CPU" build of the TidyManaged .NET wrapper library to GitHub: https://github.com/markbeaton/TidyManaged/downloads – Mark Beaton Jun 10 '11 at 01:48
2

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

Nick Martyshchenko
  • 4,231
  • 2
  • 20
  • 24
  • I've heard of and have used HtmlAgilityPack a lot in the past..but can it tidy up HTML? – mpen Oct 23 '10 at 03:43
  • HAP is not a replacement for Tidy rather it can build DOM for you and you can process it accordingly. Also Im not sure is it smart enough to parse malformed HTML (if you have to process something weird). BTW, can you define a bit better what you mean by "clean", which rules have to be applied? Also you can use original HTML Tidy (http://bit.ly/aahXs8) without rely on wrapper if you just need to clean some files not on regular basis. – Nick Martyshchenko Oct 23 '10 at 03:51
  • 1
    I don't need to to process the DOM, I just want to indent it. I specifically want a C# version because I need to use it in my C# project. I'm generating some HTML as a string, I want to take that string, have it indented, and output another string. No more, no less. Thought it would be easy to find a library to do that. – mpen Oct 23 '10 at 03:56
  • That codeproject looks nice, but it doesn't compile either. DLL linker errors. – mpen Oct 23 '10 at 04:33
  • Also, what DLL do I need to reference to access HtmlTextWriter? I can't find it anywhere in VS2010. System.Web.UI doesn't exist. – mpen Oct 23 '10 at 04:34
  • Probably your app is target to client profile? You have to switch it to full and reference System.Web.dll – Nick Martyshchenko Oct 23 '10 at 04:36
  • There are also XhtmlTextWriter Class http://bit.ly/9VlCND, since you have to output XHTML – Nick Martyshchenko Oct 23 '10 at 04:39
  • Ahh... good call with the client profile. I'm going to look at the HtmlWriters. – mpen Oct 23 '10 at 05:41
1

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.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
1

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>
educoutinho
  • 869
  • 9
  • 16
0

Beautifier provides html I used html-beautify. for example

const beautified = html_beautify("<div><p></p></div>");
console.log(beautified)
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify-html.min.js"></script>
Saeed As
  • 9
  • 3
  • Welcome to Stackoverflow. This question is about C#. Your answer seems to suggest a JS library. Please keep your answers on-topic. – mpen Jul 10 '21 at 20:23