2

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?

2
  • What's generating the html? is HtmlTextWriter an option? Jan 31, 2010 at 15:19
  • Do you mean an automated, or a manual tool?
    – Pekka
    Jan 31, 2010 at 15:19

5 Answers 5

5

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.

0
2

Here's an online version of Tidy

1
  • Saved my life, been looking for something like this for hours!
    – Liam
    Mar 19, 2013 at 16:53
1

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

0

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

0

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.