Domanda

I would like to replace instances of copyright symbols with their respective html entities. As it is now, the copyright symbols were merely copied directly into the product names, so now, when I send an email with the complete order summary, the copyright symbols are appearing as "?" instead. I need to look for (c), (R), and TM copyright symbols in within a string and replace each one with the proper HTML entity code. However, I am not sure how to do such a String.Replace() (searching for 3 characters and replacing them with 3 new values seems complicated for me)

Here is the code for setting up an email message (right now I am only checking for the (R) symbol):

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("®", "&reg;") +"</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());
                }

        }
    }

And here is the code for creating the lit variable's text value:

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());
È stato utile?

Soluzione 2

You can chain the Replace() method for every Replace operation.

Try this:

StringBuilder text = new StringBuilder(lit.Text.ToString());
msg.Body = text.Replace("value1", "replace1").Replace("value2", 
                      "replace2").Replace("value3", "replace3");

Altri suggerimenti

If you want to encode this kind of character, you should use a html encoder instead.

In C#, you can use this :

string result = System.Web.HttpUtility.HtmlEncode("Sample string ©");

If you want to replace every character one by one, you can use the code of Sudhakar Tillapudi. His answer is valid, but you are going to do a lot of replace.

The website DotNetPerls has a good example of HtmlEncode :

using System;
using System.Net;

class Program
{
    static void Main()
    {
        string a = WebUtility.HtmlEncode("<html><head><title>T</title></head></html>");
        string b = WebUtility.HtmlDecode(a);

        Console.WriteLine("After HtmlEncode: " + a);
        Console.WriteLine("After HtmlDecode: " + b);
    }
}

Output :

After HtmlEncode:
&lt;html&gt;&lt;head&gt;&lt;title&gt;T&lt;/title&gt;&lt;/head&gt;&lt;/html&gt;

After HtmlDecode:
<html><head><title>T</title></head></html>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top