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