1

i have this html tag:

<img src="/Student/ExaminationImage?name=figuur" />

and i want to strip it in just this :/Student/ExaminationImage?name=figuur and a second string with : figuur

How do i do this?

I tried everything but nothing works well.

Grtz

Ruben
  • 1,033
  • 3
  • 12
  • 22
  • 1
    Just in case you're leaning towards regex, don't. See http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 =) – 3Dave May 13 '11 at 14:16

3 Answers3

6

The Html Agility Pack is a good tool for parsing HTML.

carla
  • 1,970
  • 1
  • 31
  • 44
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • Yes i see, this could be fine. but what i have is a string. And not a document.. So i have a string with al this html code in it... – Ruben May 14 '11 at 10:44
2

You could always use linq to xml if it's always well formed XML

string imageTag = "<img src=\"\/Student\/ExaminationImage?name=figuur\" />"

string src = XElement.Parse(imageTag ).Attribute("src").Value;
Russell Troywest
  • 8,635
  • 3
  • 35
  • 40
0

To expand on @Albin's answer:

The HTML Agility Pack is a very robust, reliable way to handle this, and really is the way to go.

However, if and only if you can guarantee that your tag is a) already isolated in a string, and b) always of the same format as you've described, you can use this:

    static void Main(string[] args)
    {
        var tag = @"<img src=""/Student/ExaminationImage?name=figuur"" />";

        Console.WriteLine("Tag: {0}", tag);

        var tagParts = tag.Split(new[] {'"'},StringSplitOptions.RemoveEmptyEntries);

        var src = tagParts[1];

        Console.WriteLine("Src: {0}", src);

        var srcParts = src.Split('?');

        Console.WriteLine("Parameters: {0}", srcParts[1]);

        Console.ReadLine();
    }
3Dave
  • 28,657
  • 18
  • 88
  • 151