Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to extract <IMG> tags from HTML file?

Posted on 2003-11-23
4
Medium Priority
?
607 Views
Last Modified: 2013-11-19
Hello everyone:

anyone can tell me how to extract <IMG> tags from HTML file by using c#.net?
maybe using XML's parse function, I am not sure. please help me! thanks!

brownsbay

0
Comment
Question by:brownsbay
2 Comments
 
LVL 6

Accepted Solution

by:
purpleblob earned 60 total points
ID: 9809332
If the HTML is well formed (i.e. start and end tags) then you could load the HTML into an XML DOM and find all the img elements, however this is probably not the case, so a very simple alternative is to use the string class methods such as IndexOf. Are you actually wishing the extract, i.e. remove the <img> tags or simply find all of them ? If wishing to remove then obviously you will need to find the start <img> and it's end </img> and Remove (extract) the element.

If wishing to extract the <img> tags then unfortunately the string class is not very efficient with operations such as Remove, so you might wish to build an ArrayList of the start/end indices of the tags in the string then copy out the bits you want to keep into a StringBuilder - it's a shame StringBuilder has a Remove method but not Find or IndexOf - ah well we can't have it all :-)
0
 
LVL 10

Assisted Solution

by:ptmcomp
ptmcomp earned 60 total points
ID: 9812004
You can use SGML: http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=b90fddce-e60d-43f8-a5c4-c3bd760564bc

or Regex:

Matches matches = Regex.Matches(html, "<img.*?>");
foreach(Match match in matches)
{
     Console.WriteLine(Match.Value);
}
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question