3

I am getting this HTML string from DB :-

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex</p><img class="classname" alt="alttext" src="http://www.domain.com/uploads/myimage.jpg" width="612" height="612" /><p>Going by the Itinerary, we will be at the official launch on the 22nd May.</p><img class="classname" alt="alttext" src="http://www.domain.com/uploads/myimage1.jpg" width="612" height="612" />

As you can see that in string there is two image tags. I want to get the first image tag's source for eg :-

http://www.domain.com/uploads/myimage.jpg

can anyone suggest me how can get this text from html string.

Thanks in advance

3 Answers 3

14

You can use an html parser like HtmlAgilityPack for this

string html = .......
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var link = doc.DocumentNode.SelectSingleNode("//img").Attributes["src"].Value;
Sign up to request clarification or add additional context in comments.

3 Comments

Spike beated you by 7 minutes ;)
@dasheddot So? Instead of giving link-only answer, I posted also a working code specific to the OP's case.
@Edit reviewers - the changed URL is valid; visit the initial link and see the project is moved to a domain, pointed to by the initial source.
3

I would recommend HTML Agility pack: http://htmlagilitypack.codeplex.com/wikipage?title=Examples There's an example even showing how to do it.

Comments

1

Use string.Substring to find the word src.

Remember Position of its occurrence.

Then again You can also use this in order to check, when the "" embedded string ends.

Comments

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.