HTML Agility Pack get all input fields

China☆狼群 提交于 2019-12-10 16:32:24

问题


I found some code on the internet that finds all the href tags and changes them to google.com, but how can I tell the code to find all the input fields and put custom text in there?

This is the code I have right now:

HtmlDocument doc = new HtmlDocument();
doc.Load(path);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
    HtmlAttribute att = link.Attributes["href"];
    att.Value = "http://www.google.com";
}
doc.Save("file.htm");

Please, can someone help me, I can't seem to find any information about this on the internet :(.


回答1:


Change the XPath selector to //input to select all the input nodes:

foreach (HtmlNode input in doc.DocumentNode.SelectNodes("//input"))
{
    HtmlAttribute att = input.Attributes["value"];
    att.Value = "some text";
}



回答2:


Your current code selected all a elements (that have a href attribute): "//a[@href]".

You want it to select all input elements: "//input".

Of course, the inner part of the loop will need to change to match what you are looking for.

I suggest you read up on XPath.



来源:https://stackoverflow.com/questions/12581227/html-agility-pack-get-all-input-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!