HTML Agility Pack get all input fields

c# html-agility-pack

Question

On the internet, I discovered a code that retrieves all thehref tags and changes them to google.com, but how do I instruct the algorithm to locate all theinput fields and insert your own text there?

The code I now have is as follows:

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");

Could someone please assist me? I can't seem to locate any information about this online.

1
4
9/25/2012 10:43:33 AM

Accepted Answer

XPath selector changed to//input to choose every input node:

foreach (HtmlNode input in doc.DocumentNode.SelectNodes("//input"))
{
    HtmlAttribute att = input.Attributes["value"];
    att.Value = "some text";
}
6
9/25/2012 10:43:59 AM

Popular Answer

All were chosen by your current code.a Element(s) with ahref attribute): "//a[@href]" .

You want it to choose everything.input elements: "//input" .

Naturally, the inner portion of the loop will need to be modified to fit your needs.

I advise you to research XPath.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow