title: C# version of HTML Tidy?
body: <p>I am just looking for a really easy way to clean up some HTML (possibly with embedded JavaScript code). I tried <a href="http://tidyfornet.sourceforge.net/" rel="noreferrer">two</a> <a href="http://users.rcn.com/creitzel/tidy.html" rel="noreferrer">different</a> <a href="http://en.wikipedia.org/wiki/HTML_Tidy" rel="noreferrer">HTML Tidy</a> .NET ports and both are throwing exceptions...</p>
  
  <p>Sorry, by "clean" I mean "indent". The HTML is not malformed, at all. It's <a href="http://en.wikipedia.org/wiki/XHTML" rel="noreferrer">XHTML</a> strict.</p>
  
  <hr>
  
  <p>I <em>finally</em> got something working with <a href="http://en.wikipedia.org/wiki/Standard_Generalized_Markup_Language" rel="noreferrer">SGML</a>, but this is seriously the most ridiculous chunk of code ever to indent some HTML.</p>
  
  <pre><code>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();
  }
  </code></pre>