【发布时间】:2014-02-28 14:51:50
【问题描述】:
我想用它们各自的 html 实体替换版权符号的实例。现在,版权符号只是直接复制到产品名称中,所以现在,当我发送包含完整订单摘要的电子邮件时,版权符号显示为“?”反而。我需要在字符串中查找 (c)、(R) 和 TM 版权符号,并将每个符号替换为正确的 HTML 实体代码。但是,我不确定如何执行这样的 String.Replace()(搜索 3 个字符并用 3 个新值替换它们对我来说似乎很复杂)
这是设置电子邮件的代码(现在我只检查 (R) 符号):
protected void sendEmail()
{
Item CurrentItem = Sitecore.Context.Item;
Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoOrderReview");
if (HomeItem != null)
{
TextBox rTxt = (TextBox)FindControl("rEmail");
TextBox fName = (TextBox)FindControl("yName");
Literal lit = (Literal)FindControl("emailTbl");
//string html = lit.Text;
try
{
//.Value = string.Format("<tr><td style="width:80px;">{0}</td><td style="width:50px;">{1}</td><td style="width:20px;>{2}</td><td style="width:20px;>{3}</td><td style="width:60px;>{4}</td></tr>", SkuItem.Fields["Order Number"].Value, SkuItem.Fields["Description"].Value, qtyAmt);
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(rTxt.Text);
msg.Subject = "GOJO product order summary";
msg.From = new System.Net.Mail.MailAddress("donotreply@GOJO.com");
msg.Body = "Here is a list of the products your friend, " + fName.Text + ", has selected: ";
msg.IsBodyHtml = true;
msg.Body += "<h1></h1>";
msg.Body += "<table cellpadding='4' cellspacing='0' border='1px solid black' style='padding: 4px 0 4px 0; border-collapse: collapse; width:750px; text-align: left; font-family: sans-serif;'><tr><th></th><th>Product</th><th>GOJO SKU</th><th>Size</th><th>Case Pack</th><th>Quantity</th></tr>";
msg.Body += lit.Text.ToString().Replace("®", "®") +"</table>";
System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient("mail.innismaggiore.com");
sc.Send(msg);
}
catch (Exception EX)
{
GOJOHelper.WriteLog("GOJO Order Review - sendEmail()", EX.ToString());
}
}
}
下面是创建 lit 变量文本值的代码:
lit.Text += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>", drow["Thumbnail"], CPNItem["Complete Name"], marketItem.Fields["SKU"].Value, drow["Size"].ToString(), marketItem.Fields["Case Pack"].Value, qtys.ToString());
【问题讨论】:
-
您可以将调用链接到 String.Replace。对于这类问题,正则表达式会带来更多麻烦。
-
你不应该链接
String.Replace调用,因为每个调用都会遍历原始字符串并创建一个新字符串。 -
考虑使用
StringBuilder来构建body body。每个字符串 += 连接都会产生一个新字符串。 -
是的,确实如此,但大多数时候性能影响可以忽略不计。
标签: c# html regex replace html-entities