2011-01-14 113 views
0

我想提取所有图像链接,以便我可以自由使用所有图像。如何在asp.net C#从HTML文本字符串中提取图像链接

<div> 
    <img src="/upload/Tom_Cruise-242x300.jpg" alt="Tom_Cruise-242x300.jpg" align="left" border="0" height="300" width="242"> 
    sample text sample text sample text sample text 
    <img src="http://www.sharicons.com/images/rss_icon.jpg" alt="Icon" align="left" border="0" height="100" width="100"> 
    sample text sample text sample text sample text sample text sample text sample text sample text</div> 

做给我的解决方案

string ProcessedText = Regex.Replace(sb.ToString(), "^<img[^>]*>", string.Empty); 

回答

1

我会使用HTML Agility Pack。
然后,你可以做这样的事情:

HtmlNodeCollection allImages = doc.DocumentNode.SelectNodes("//img[@src]"); 
0

一个简单的方法做,这是把字符串转换成一个名为myString字符串,然后运行下面的代码:

List<string> imagePaths = new List<string>(); 
while(myString.IndexOf("img src=") >= 0){ 
    myString = myString.Substring(myString.IndexOf("img src=")+9); 
    imagePaths.Add(myString.Substring(0,myString.IndexOf("\""))); 
} 

列表imagePaths现在将包含所有图像链接。

0

您可以使用HTMLAgilityPack或第二个选项是正则表达式:)