string
ドキュメントのhtmlコードを含むがあります。
中に入れることができますmultiple image tags
。
私がやりたいのは、imgタグのsrc
属性値、つまりurlをC#関数に渡し、その値を関数returnsに置き換えることです。
これどうやってするの?
正規表現は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