1
votes

I have to parse the html of an email, get all the img tags and replace the src value with the url of the image I want.

This part is already done. I can access the <img> and change the attribute with the url I want.

The problem is that when I print the html the src equals cid:companylogo and it should be the full url of the image I gave.

I need to know how to load the html after the src values of the <img> in the original html are changed. The below code receives as a parameter the original html of the email in a string which is "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;
1
What is the item variable you are referring to in the line before the return? - Oded
its received also as a parameter the call to this function is inside this block: foreach (Item item in findResults.Items) - rdk1992
the point is i dont know how to access the html content that has suffer the change in the src values of the <img> - rdk1992
You are not changing item at all, but email. Why use the object you have not changed? - Oded
yes i see that, strange thing is there is no email.body in my documentation , i have the latest version of htmlagilitypack - rdk1992

1 Answers

1
votes

As you note in your comments, the item variable you are using is also passed in - however, this isn't what you are manipulating.

Instead of:

body = item.Body;

You should use:

body = email.Body;

Update:

Since you have now revealed you are using the HAP, you can simply get the inner HTML of the document node.

Untested:

body = email.DocumentNode.InnerHtml;