如何使用C#从给定的HTML字符串得到IMG标记的源 [英] How to get IMG tag's source from given HTML string using c#

查看:110
本文介绍了如何使用C#从给定的HTML字符串得到IMG标记的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库得到这个HTML字符串: -

I am getting this HTML string from DB :-

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

正如你可以看到,在字符串有两个图像标记。我想要得到的第一个图像标签的源如: -

As you can see that in string there is two image tags. I want to get the first image tag's source for eg :-

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

任何人都可以建议我怎么可以从HTML字符串这个文本。

can anyone suggest me how can get this text from html string.

在此先感谢

推荐答案

您可以使用HTML解析器像 HtmlAgilityPack 以这种

You can use an html parser like HtmlAgilityPack for this

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

这篇关于如何使用C#从给定的HTML字符串得到IMG标记的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆