Html Agility Pack InnerHtml returns incorrect string with textboxes
See the question and my original answer on StackOverflowThis is not a bug. INPUT is considered by the parser as an "empty" element (see this for example: HTMLAgilityPack don't preserves original empty tags on the empty elements subjects), and by default, such elements are rendered without the closing /.
The reasons are historically related to HTML 3.2. Back in those days, INPUT was not required to be closed, although it looks like like a bug today.
This will fix your problem:
public void PossibleHtmlAgilityPackBug()
{
const string html = @"<input type=""text"" name=""shouldNotTrim"" />";
var doc = new HtmlDocument();
doc.OptionWriteEmptyNodes = true;
doc.LoadHtml(html);
Assert.That(doc.DocumentNode.InnerHtml, Is.EqualTo(html));
}
As a side note, the HTML agility pack will not always create an exact equivalent of the html text, but it will always try to rebuild something that will be rendered the same way. Browsers support an unclosed INPUT without a problem.