开发者

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开发者_StackOverflow社区 tried everything but nothing works well.

Grtz


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


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;


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜