如何在CsQuery中更改所有imageTag src路径

时间:2015-05-14 16:50:23

标签: c# image src csquery

如何更改所有图像" 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;

      }
   }

2 个答案:

答案 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);