Tutorial :Need tool to format html (indent, add whitespace)


I am working on a .net project that generates html.

When the html string is generated, the is no whitespace or indenting. This makes understanding the generated html difficult.

Is there a tool that will take my string of generated html and format it so that it looks nice?


If you're generating the HTML yourself, it should be valid XML.

Therefore, you can use the XDocument class to format it.

You can build the HTML inside an XDocument, then call ToString(), which will automatically format the HTML for you.

In addition, XDocument should be much easier to use than manual string concatenation, and will intrinsically protect you from most (but not all) XSS attacks.


Here's an online version of Tidy


You might be interested in taking a look at Tidy, http://tidy.sourceforge.net/


You could use HTMLTextWriter and call HTMLTextWriter.Indent to set the indention of the lines.


using https://github.com/AngleSharp/AngleSharp

var parser = new HtmlParser();  var document = parser.Parse(html);    using (var writer = new StringWriter())  {      document.ToHtml(writer, new PrettyMarkupFormatter());      return writer.ToString();  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »