I’m using the HtmlAgilityPack and trying to extract an image name from html. Here’s the html string I have:
sHtml = "<HTML><HEAD></HEAD><BODY>Here are some images.</br>1) < IMG style='MARGIN-BOTTOM: 20px; MARGIN-LEFT: 20px' align=right src='images/sample001.jpg'>2) < IMG style='MARGIN-BOTTOM: 25px; MARGIN-LEFT: 25px' align=right src='images/sample002.png'></br> And some docs as well.</br>1) href='javascript:parent.POPUP({url:'testDoc001.htm',type:'shared',width:600,height:645})'></br>2) href='javascript:parent.POPUP({url:'testDoc002.html',type:'shared',width:700,height:712})'></br></BODY></HTML>"
In WPF C# I pass this string into the following routine:
private static List<string> ExtractHtmlInfo(string sHtml)
{
HtmlDocument doc = new HtmlDocument();
doc.Load(new StringReader(sHtml));
HtmlNode root = doc.DocumentNode;
List<string> anchorTags = new List<string>();
//foreach (HtmlNode link in root.SelectNodes("//a"))
foreach (HtmlNode link in root.SelectNodes("//img"))
{
string att = link.OuterHtml;
anchorTags.Add(att);
}
return anchorTags;
}
When I step through the code I see that the line:
string att = link.OuterHtml;
provides the entire < img node … which is more than I want.
I would like anchorTags to have just the folder and name of the file, as in:
[0] = images/sample001.jpg
[1] = images/sample002.png
So, I need something other than .OuterHtml but cannot find it.
Can anyone help?
You are looking for the values of the
srcattributes of the image elements: