3

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" />

ご覧のとおり、文字列には 2 つのイメージ タグがあります。たとえば、最初のイメージタグのソースを取得したい:-

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

HTML文字列からこのテキストを取得する方法を教えてください。

前もって感謝します

4

3 に答える 3

13

これには、HtmlAgilityPackのような html パーサーを使用できます。

string html = .......
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var link = doc.DocumentNode.SelectSingleNode("//img").Attributes["src"].Value;
于 2013-05-21T07:44:01.577 に答える
3

HTML Agility パックをお勧めします: http://htmlagilitypack.codeplex.com/wikipage?title=Examples方法を示す例もあります。

于 2013-05-21T07:37:03.613 に答える
1

string.Substringを使用して単語を検索しますsrc

その発生位置を覚えておいてください。

次に、これを使用して、埋め込まれた文字列がいつ終了するかを確認することもできます。

于 2013-05-21T07:40:27.597 に答える