c# - Add form tag around a body tag using HtmlAgilityPack -
how add form tag around hmldocument?
for given html
<html> <head> <title></title> </head> <body> <p>full name: <input name="fullname" type="text" value=""></p> <p><input name="btnsubmit" type="submit" value="submit"></p> </body> </html>
and following code...
var doc = new htmldocument(); doc.optionautocloseonend = true; doc.loadhtml(input); var body = doc.documentnode.selectsinglenode("//body"); if (doc.documentnode.selectnodes("//form[@action]") == null) { var form = doc.createelement("form"); form.attributes.add("action", "/pages/event/10302"); body.prependchild(form); } return doc.documentnode.outerhtml;
i following, notice don't have closing </form>
tag above closing </body>
tag
<html> <head> <title></title> </head> <body> <form action="/pages/event/10302"> <p>full name: <input name="fullname" type="text" value=""></p> <p><input name="btnsubmit" type="submit" value="submit"></p> </body> </html>
the form element has special treatment. see here on more: htmlagilitypack -- <form> close reason?
so, this:
var doc = new htmldocument(); htmlnode.elementsflags.remove("form"); // remove special handling form doc.loadhtml(input); var body = doc.documentnode.selectsinglenode("//body"); if (doc.documentnode.selectnodes("//form[@action]") == null) { var form = doc.createelement("form"); form.attributes.add("action", "/pages/event/10302"); body.prependchild(form); }
but this:
<html> <head> <title></title> </head> <body> <form action="/pages/event/10302"></form> <p>full name: <input name="fullname" type="text" value=""></p> <p><input name="btnsubmit" type="submit" value="submit"></p> </body> </html>
which logical, don't surround in new form. so, instead can this:
var doc = new htmldocument(); doc.loadhtml(input); var body = doc.documentnode.selectsinglenode("//body"); if (doc.documentnode.selectnodes("//form[@action]") == null) { var form = body.clonenode("form", true); form.attributes.add("action", "/pages/event/10302"); body.childnodes.clear(); body.prependchild(form); }
which this:
<html> <head> <title></title> </head> <body><form action="/pages/event/10302"> <p>full name: <input name="fullname" type="text" value=""></p> <p><input name="btnsubmit" type="submit" value="submit"></p> </form></body> </html>
this not way, works, , don't have remove form special treatment.
Comments
Post a Comment