开发者

Finding first Image from a content

How can I find an image from a content? I have a method in aspx I am calling this method for remove all html 开发者_开发问答tags like this: Usage.DeleteHtml(Eval("content").ToString())

but I don't want delete img tag from content.. I should find the first image I will show it on my page.. like this:<a href="#"><img src="Usage.FindImage("content")" /></a>

but couldn't write a method for finding image..

my DeleteHtml method:

public static string DeleteHtml(string text)
{
    string mystr= Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
    return mystr;
}


I assume that your task is essentially retrieving the first image in document. If your HTML document is a well-formed XML-document as well, you could easily solve your task using XPath.

More on XPath in .NET here.

XPath query to retrieve the first image's URL will look like this:

//img[1]/@src

Otherwise, if you really need to strip HTML, it's a duplicate to a couple of questions already:

Using C# regular expressions to remove HTML tags

How can I strip HTML tags from a string in ASP.NET?

How to clean HTML tags using C#

Short answer: use Html Agility Pack.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜