更改src属性

我必须解析一封电子邮件的html,获取所有的img标签,并将src值替换为我想要的图像的url。

这部分已经完成。 我可以访问并使用我想要的url更改属性。

问题是,当我打印html时,src等于cid:companylogo ,它应该是我给出的图像的完整URL。

我需要知道如何在原始html中的的src值更改后加载html。 下面的代码作为参数接收电子邮件的原始html,字符串是“body”

  string SRC = ""; int indice = 0; //Console.WriteLine(body); HtmlDocument email = new HtmlDocument(); email.LoadHtml(body); foreach (HtmlNode img in email.DocumentNode.SelectNodes("//img")) { SRC = img.GetAttributeValue("src", null); for (int i = 0; i < contentIDS.Count; i++) { if (SRC.Equals(contentIDS[i])) { indice = i; break; } } img.SetAttributeValue("src", urls[indice].ToString());//change src value Console.WriteLine("URL" + img.GetAttributeValue("src", null));//its printed how i want to } body = item.Body;//am stuck here i want body to have a final value of the whole html but with the changes made in src above. return body; 

正如您在评论中注意到的那样,您正在使用的item变量也会被传入 – 但是,这不是您正在操作的内容。

代替:

 body = item.Body; 

你应该使用:

 body = email.Body; 

更新:

由于您现在已经发现您正在使用HAP,因此您只需获取文档节点的内部HTML即可。

未经测试:

 body = email.DocumentNode.InnerHtml;