How to produce indented HTML using HtmlAgilityPack?

c# html-agility-pack html-generation

Question

HtmlAgilityPack is what I'm using to generate HTML, and it works well, however the html content is not indented. But I need HTML; I can obtain indented XML. Exists a path?

HtmlDocument doc = new HtmlDocument();

// gen html
HtmlNode table = doc.CreateElement("table");
table.Attributes.Add("class", "tableClass");
HtmlNode tr = doc.CreateElement("tr");
table.ChildNodes.Append(tr);
HtmlNode td = doc.CreateElement("td");
td.InnerHtml = "—";
tr.ChildNodes.Append(td);

// write text, no indent :(
using(StreamWriter sw = new StreamWriter("table.html"))
{
        table.WriteTo(sw);
}

// write xml, nicely indented but it's XML!
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlWriter xw = XmlTextWriter.Create("table.xml", settings))
{
        table.WriteTo(xw);
}
1
9
3/25/2014 3:16:18 AM

Accepted Answer

I don't believe HTMLAgilityPack is capable of doing this. However, you might peruse the html tidy bundles suggested in inquiries like these:

3
5/23/2017 10:30:55 AM

Popular Answer

No, and it was done "by design." There is a significant distinction between HTML and XML, where whitespace often has no particular significance (or XHTML, which is XML, not HTML).

This is not a small improvement, since altering the whitespaces may affect how certain browsers display a particular HTML piece, particularly when the HTML is incorrect (that is in general well handled by the library). Furthermore, the HTML Agility Pack was created to preserve the rendered structure of HTML rather than to decrease the written structure of markup.

I'm not saying it's just not possible or even conceivable. Of course, you could convert to XML and be done with it (and you might create an extension method to make this easy), but in most cases, the displayed result could change.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow