-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?

5
  • 2
    The <center> cannot hold it is too late. Commented Oct 30, 2012 at 13:39
  • 5
    If you could post what you tried so far, it might be easier to understand. Commented Oct 30, 2012 at 13:39
  • Check this question: stackoverflow.com/questions/4257359/… Commented Oct 30, 2012 at 13:44
  • -1 this question has been asked here many many times! Commented Oct 30, 2012 at 13:46
  • 1
    You can't parse HTML with regex Commented Oct 30, 2012 at 13:57

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@DevendraGohil use doc.DocumentNode.OuterHtml to get the full html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.