C#修改HTML内容

Lor*_*ion 0 html c# jquery

我正在尝试发送带有 HTML 正文的电子邮件,在发送邮件之前我想修改一些 HTML。基本上就像在 jquery 中一样,我需要更改具有某些 ID 的 div 内的一些文本值。

我尝试使用https://github.com/jamietre/CsQuery但我没有找到有关 ID 选择器的任何信息。

有人可以推荐任何其他解决方案吗?

这是我现在的 sendEmail 代码:

public static void sendEmail(String reciverMail, String Subject1, String Body1)
{
    if (reciverMail.Contains('@') && reciverMail != "")
    {
        using (StreamReader reader = File.OpenText(HttpContext.Current.Server.MapPath("~/mail.htm")))
        {

            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = false;
            client.Credentials = SmtpServerCredentials;
            client.Host = SmtpServerAddress;
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(SenderAddress, SenderName1);
            mail.To.Add(reciverMail);
            mail.Subject = Subject1;
            mail.IsBodyHtml = true;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            //mail.Body = Body1;
            mail.Body = reader.ReadToEnd();

            try
            {
                client.Send(mail);
            }
            catch (SmtpException ex)
            {
                string aaa = ex.Message;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

OuS*_*uSs 5

您可以使用Html Agility Pack:这是一个可爱的 HTML 解析器,通常推荐用于此类工作。

然后,您可以编写以下内容:

HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml("Your content");
string centralDiv = htmlDocument.DocumentNode.SelectSingleNode("//div[@id='centralDiv ']").InnerHtml;
Run Code Online (Sandbox Code Playgroud)