更改src属性<img/>

时间:2012-02-21 16:57:36

标签: c# html parsing

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

这部分已经完成。我可以访问<img>并使用我想要的网址更改属性。

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

我需要知道在更改原始html中<img>的src值后如何加载html。以下代码以字符串“body”

作为参数接收电子邮件的原始html
        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;

1 个答案:

答案 0 :(得分:1)

正如您在评论中所述,您传递的item变量也会被传入 - 但是,这不是您正在操作的。

而不是:

body = item.Body;

您应该使用:

body = email.Body;

更新

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

未测试:

body = email.DocumentNode.InnerHtml;
相关问题