-1

I have a string that contains html code of a document.

It can have multiple image tags inside.

What I want to do is to pass the img tag's src attribute value i.e. url to a C# function and replace that value with the function returns.

How can I do this?

Anirudha
  • 32,393
  • 7
  • 68
  • 89
Dev
  • 6,570
  • 10
  • 66
  • 112

1 Answers1

3

Regex is not a good for parsing HTML files.HTML is not strict nor is it regular with its format.(for example: in non strict html its OK to have a tag without a closing tag)

Use htmlagilitypack

You can use htmlagilitypack to do it like this

HtmlDocument doc = new HtmlDocument();
doc.Load(yourStream);

foreach(var item in doc.DocumentNode.SelectNodes("//img[@src]"))//select only those img that have a src attribute..ahh not required to do [@src] i guess
{
 item.Attributes["src"].Value=yourFunction(item.Attributes["src"].Value);
}
doc.Save("yourFile");//dont forget to save
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • I am passing HTML in string and I want to return that string with replaced values. I don't want to save it in file. How do I do it? – Dev Oct 31 '12 at 05:00
  • @DevendraGohil use `doc.DocumentNode.OuterHtml` to get the full html – Anirudha Oct 31 '12 at 07:06