简体   繁体   English

如何使用HTML Agility Pack向表单添加输入标记?

[英]How do I add an input tag to a form using HTML Agility Pack?

I start off with a set of forms: 我从一组表格开始:

<form action="/Test/1">
    <p>This is my 1st form.</p>
    <button type="submit">Submit Form</button>
</form>

<form action="/Test/2">
    <div>This is my 2nd form.</div>
    <input type="submit" value="Submit Form" />
    <input type="hidden" name="var1" value="123" />
</form>

And I attempt to add an input tag to all forms on the page: 我尝试在页面上的所有表单中添加一个输入标记:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(inputHtml); // inputHtml is a string containing the forms above
var nodes = doc.DocumentNode.SelectNodes("//form");
if (nodes != null)
{
    foreach (var node in nodes)
    {
        node.AppendChild(HtmlNode.CreateNode("<input type=\"hidden\" name=\"var2\" value=\"456\" />"));
    }
}
var model = doc.DocumentNode.OuterHtml;

This is what I was expecting: 这就是我所期待的:

<form action="/Test/1">
    <p>This is my 1st form.</p>
    <button type="submit">Submit Form</button>
    <input type="hidden" name="var2" value="456">
</form>

<form action="/Test/2">
    <div>This is my 2nd form.</div>
    <input type="submit" value="Submit Form">
    <input type="hidden" name="var1" value="123">
    <input type="hidden" name="var2" value="456">
</form>

However, this is what I get as a result: (line breaks and spacing added for readability) 但是,这就是我得到的结果:( 添加了换行符和间距以提高可读性)

<form action="/Test/1">
    <input type="hidden" name="var2" value="456">
</form>
    <p>This is my 1st form.</p>
    <button type="submit">Submit Form</button>
</form>

<form action="/Test/2">
    <input type="hidden" name="var2" value="456">
</form>
    <div>This is my 2nd form.</div>
    <input type="submit" value="Submit Form">
    <input type="hidden" name="var1" value="123">
</form>

Now I have duplicate </form> closing tags. 现在我有重复的</form>结束标记。

What is the proper way to achieve the desired result using HTML Agility Pack? 使用HTML Agility Pack实现所需结果的正确方法是什么?

Update: 2nd form in expected results had a bug in it showing the duplicate form tag. 更新:预期结果中的第二个表单中有一个错误,显示重复的表单标记。 (Copy-paste error) (复制粘贴错误)

Just call HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("form"); 只需调用HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("form"); before you load your document, after that append should work as you expect. 在加载文档之前,该附加应该按预期工作。

on a side node: 在一个侧节点上:

why bother skipping double quotes when you can write it as: 为什么打扰跳过双引号,当你可以写为:

"<input type='hidden' name='var2' value='456'/>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从嵌套中获取文本 <p> 使用html敏捷包在外部html中标记? - how do i get the text from a nested <p> tag in an external html using html agility pack? 使用html敏捷包解析表单动作以及输入名称和值 - Parsing Form action and input name and values using html agility pack 如何将html字符串转换为可与HTML Agility Pack(特别是DocumentNode.InnerText)一起使用的表单 - How do I get a html string into a form that can be used with HTML Agility Pack (and in particular DocumentNode.InnerText) 如何使用HTML Agility Pack获取标题标签 - How to get title tag using HTML Agility Pack 如何使用HTML Agility Pack获取<td>标签内的所有内容? - How can I get all content within <td> tag using a HTML Agility Pack? 如何使用HTML Agility Pack编辑HTML片段 - How do I use HTML Agility Pack to edit an HTML snippet 如何获得带有html敏捷包的自定义标签? - How get a custom tag with html agility pack? 如何使用HTML Agility Pack删除一些(或全部)HTML元素和/或属性? - How do I remove some (or all) HTML elements and/or attributes using HTML Agility Pack? 我如何使用html敏捷包从html页面中选择此数据 - How do i pick this data from an html page using html agility Pack 我如何输出个人 <p> 使用HTML Agility Pack将标签添加到富文本框中? - How do I output individual <p> tags using HTML Agility Pack to a rich text box?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM