Getting src attribute from img 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!

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
}

Popular Answer

Did you try something like this:

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



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why