如何在 HMLDocument 周围添加表单标签?
对于给定的 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>
和下面的代码......
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;
我得到以下内容,请注意我们在结束</form>
标签上方没有结束</body>
标签
<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>