改变SRC属性< IMG> [英] changing src attributes <img>

查看:146
本文介绍了改变SRC属性< IMG>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析的邮件的HTML,得到所有的img标签,并与我想要的图像的URL替换src值。



这部分已经完成。我可以访问< IMG> 和更改与我想要的网址的属性。



的问题是,当我打印的HTML中的src等于 CID:CompanyLogo的,它应该是完整的URL图像的我给。



我需要知道如何的<在src值之后加载HTML; IMG> 原HTML被改变。下面的代码作为参数接收电子邮件的原始HTML在一个字符串,它是身体

 字符串SRC = ; 
INT指数之= 0;
//Console.WriteLine(body);

的HTMLDocument电子邮件=新的HTMLDocument();
email.LoadHtml(体);

的foreach(HtmlNode IMG在email.DocumentNode.SelectNodes(// IMG))
{
SRC = img.GetAttributeValue(src用户,NULL);
的for(int i = 0; I< contentIDS.Count;我++)
{
如果(SRC.Equals(CONTENTIDS [I]))
{
指数之= I;
中断;
}
}

img.SetAttributeValue(SRC,网址[指数之]的ToString()); //改变SRC值
Console.WriteLine( URL+ img.GetAttributeValue(SRC,NULL)); //打印的我多么想
}

体= item.Body; //我坚持在这里,我想身体有整体的html但在SRC上面所做的修改的最终值。
收益主体;


解决方案

当你在你的注释注意,<$ C 。$ C>项目您正在使用中也通过变量 - 但是,这是不是你操纵什么



而不是:

  =身体item.Body; 

您应该使用:

  =身体email.Body; 



更新:



既然你现在有。发现你正在使用的HAP,你可以简单地获取文档节点的内部HTML



未经测试的:

  =身体email.DocumentNode.InnerHtml; 


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;

解决方案

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;

这篇关于改变SRC属性&LT; IMG&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆