Obtaining the src property from an image tag

asp.net c# html-agility-pack

Question

I'm using the HAP library to parse HTML: http://html-agility-pack.net

I basically just want to retrieve the src value from all the img tags.

I've tried several thing but I can't seem to do it!

1
0
11/24/2017 9:48:33 PM

Accepted Answer

Modified from the examples page:

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm"); //or whatever HTML file you have
HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]");
if (imgs == null)
   return;
foreach (HtmlNode img in imgs)
{
   if (img.Attributes["src"] == null)
      continue;
   HtmlAttribute src = img.Attributes["src"];
   //Do something with src.Value
}
3
1/6/2011 5:39:06 PM

Popular Answer

Did you try something like this:

HtmlNodeCollection images = doc.DocumentNode.SelectNodes("//img[@src]");


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