debin debin - 4 months ago 29
HTML Question

HTML agility pack ignore input slash

I have tried all answers to solve this but it didn't work for me..
code is like

HtmlNode.ElementsFlags.Remove("option");
HtmlNode.ElementsFlags.Remove("link");
HtmlNode.ElementsFlags.Remove("meta");
HtmlNode.ElementsFlags.Remove("form");
HtmlDocument html = new HtmlDocument();
HtmlNode.ElementsFlags["input"] = HtmlElementFlag.Empty;
html.OptionWriteEmptyNodes = true;
html.load(file);
var ETagTable = html.CreateElement("table");
ETagTable.InnerHtml = ChangedTagE_Str;

/* where changedtage_str = <tr>
<th>Reference:</th>
<td><input type="text" id="e.reference" size="30" maxlength="32" value="" /></td>
</tr>*/


in innerHTML is gives input as
<input type="text" id="e.crm_reference" size="30" maxlength="32" value="">


I want
<input type="text" id="e.crm_reference" size="30" maxlength="32" value="" />


any solution please?

Answer

Set OptionWriteEmptyNodes to true, modify the html in the document and parse back the resulting html by calling LoadHtml().

HtmlDocument html = new HtmlDocument();
html.OptionWriteEmptyNodes = true;
html.Load("page.htm");
html.DocumentNode.Element("html").Element("body").InnerHtml = "<input type=\"text\" />";
html.LoadHtml(html.DocumentNode.OuterHtml); //Reload the document

What Gauthaman suggested works if you're loading an external html by using Load() or LoadHtml() method but it doesn't when inserting new elements or setting innerHTML property.

What we can do is tricking Agility to reload the document.

HtmlDocument html = new HtmlDocument();
html.OptionWriteEmptyNodes = true;
html.LoadHtml("<!DOCTYPE html><html><head><title>ttt</title></head><body></body></html>");
var ETagTable = html.CreateElement("table");
html.DocumentNode.Element("html").Element("body").AppendChild(ETagTable);
ETagTable.InnerHtml = "<tr><th>Reference:</th><td><input type=\"text\" size=\"30\""
    +"maxlength=\"32\" value=\"\" /></td></tr>";
html.LoadHtml(html.DocumentNode.OuterHtml); //<-- Reload the document
Console.WriteLine(html.DocumentNode.InnerHtml);