如何更改所有图像" src"在CSQuery的html字符串中?
这是我的代码:
CQ HtmlContainingImg = html;
CQ imgTagList = HtmlContainingImg["IMG"];
foreach (var img in imgTagList)
{
string imgsrc = img.Attributes["src"];
if (!IsAbsoluteUrl(imgsrc))
{
//img.Attributes["src", Setting.FelApplicationPath + Setting.folderPath + imgsrc];// this line gives error
// even tried img.Attributes["src"]= Setting.FelApplicationPath + Setting.folderPath + imgsrc;
}
}
答案 0 :(得分:1)
这对我有用
CQ HtmlContainingImg = html;
foreach (var img in HtmlContainingImg["IMG"])
{
string imgsrc = img.Attributes["src"];
if (!IsAbsoluteUrl(imgsrc))
{
img.Attributes["src"]= Setting.FelApplicationPath + Setting.folderPath + imgsrc;
}
}
html= HtmlContainingImg.Render(); // I was missing this line
答案 1 :(得分:0)
尝试 img.SetAttribute(“src”,Setting.FelApplicationPath + Setting.folderPath + imgsrc);