簡體   English   中英

如何使用c#從給定的HTML字符串中獲取IMG標記的源代碼

[英]How to get IMG tag's source from given HTML string using c#

我從DB獲取此HTML字符串: -

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex</p><img class="classname" alt="alttext" src="http://www.domain.com/uploads/myimage.jpg" width="612" height="612" /><p>Going by the Itinerary, we will be at the official launch on the 22nd May.</p><img class="classname" alt="alttext" src="http://www.domain.com/uploads/myimage1.jpg" width="612" height="612" />

正如您在字符串中看到的那樣,有兩個圖像標記。 我想得到第一個圖像標簽的來源,例如: -

http://www.domain.com/uploads/myimage.jpg

任何人都可以建議我如何從HTML字符串中獲取此文本。

提前致謝

你可以使用像HtmlAgilityPack這樣的html解析器

string html = .......
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var link = doc.DocumentNode.SelectSingleNode("//img").Attributes["src"].Value;

我會推薦HTML Agility pack: http//htmlagilitypack.codeplex.com/wikipage? title = Example有一個例子甚至展示了如何做到這一點。

使用string.Substring查找單詞src

記住它發生的位置。

然后,您還可以使用它來檢查“”嵌入字符串何時結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM