正则表达式替换图像标签SRC属性值



i具有包含文档的HTML代码的string

它可以内部具有multiple image tags

我要做的是将IMG标签的src属性值(即URL)传递到c#函数,然后用函数返回替换该值。

我该怎么做?

REGEX不适合解析HTML文件。html并不严格,也不是常规的格式。(例如:在非严格的html中拥有一个没有关闭标签的标签)

使用htmlagilitypack

您可以使用htmlagilitypack像这样做

HtmlDocument doc = new HtmlDocument();
doc.Load(yourStream);
foreach(var item in doc.DocumentNode.SelectNodes("//img[@src]"))//select only those img that have a src attribute..ahh not required to do [@src] i guess
{
 item.Attributes["src"].Value=yourFunction(item.Attributes["src"].Value);
}
doc.Save("yourFile");//dont forget to save

最新更新