debin debin -5 years ago 274
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

HtmlDocument html = new HtmlDocument();
HtmlNode.ElementsFlags["input"] = HtmlElementFlag.Empty;
html.OptionWriteEmptyNodes = true;
var ETagTable = html.CreateElement("table");
ETagTable.InnerHtml = ChangedTagE_Str;

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

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 Source

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.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");
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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download