Ruben
Ruben

Reputation: 1093

Get tag out of html with C#?

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

Upvotes: 1

Views: 960

Answers (3)

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47068

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

Upvotes: 6

3Dave
3Dave

Reputation: 29071

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();
    }

Upvotes: 0

Russell Troywest
Russell Troywest

Reputation: 8776

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;

Upvotes: 2

Related Questions