Convert characters to web safe

I'm taking some input from the user using a standard <asp:textbox...

I'm creating a textfile from the contents of the textbox and save it as .html. This works fine, however if the user enters some dodgy characters, such as this: “cover story.”

This turns the html into looking like this: “cover story.â€

If i open the file up in notepad and then save it all is fixed.

How can i get around this issue?  
LVL 9
Type25Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lazyberezovskyCommented:
Man, if you do following:
using (StreamWriter writer = File.CreateText(Server.MapPath("~/test.html")))        
    writer.WriteLine(TextBox1.Text);

You will not receive encoding problems.
So, what is the way you are saving textbox text?
0
Carl TawnSystems and Integration DeveloperCommented:
Can you show the code you are using to write to file? It sounds like a problem with the Encoding.
0
Type25Author Commented:
Excuse the formatting
                do{
                
                    int randNumber = RandomNumber(minRand, maxRand);
                    htmlFilename = randNumber + "_article.html";

                    if (!File.Exists(articleLocation + htmlFilename))
                    {
                        // Create HTML File

                        TextWriter tw = new StreamWriter(articleLocation + htmlFilename);

                        tw.WriteLine(@"<html><title>" + articleTitle + "</title>");
                        tw.WriteLine(@"<meta name=""apple-mobile-web-app-capable"" content=""yes""><meta name = ""viewport"" content = ""initial-scale = 1.0,  user-scalable = no"">");
                        tw.WriteLine(@"<style>body { width:310px; } .copy {font-family:Calibri,Arial; font-size:16px;background:url(../MagWatermarks/" + customerName + ".gif);  }h2 {font-family:Arial,Calibri;font-size:26px; font-weight:none; } .caption {color:Gray;font-size:13px;font-family:Arial;}</style>");
                        tw.WriteLine(@"<h2>" + articleTitle + "</h2>");
                        tw.WriteLine(@"<img src='" + picLocation + "iphone-" + articleImageURL + "' style='padding-left:2px'>");
                        tw.WriteLine(@"<span class=caption>" + caption + "</span>");
                        tw.WriteLine(@"<br><br><div class=copy>" + articleCopy + "</span>");
                        tw.WriteLine(@"<div style=""font-family:Calibri;width:304;height:30px;background:WhiteSmoke;padding-top:4px;font-family:Calibri;text-align:center""><a href='" + linkURL + "'>" + linkName + "</a></div>");

                        tw.Close();

                        done = true;

                    }

                }
                while (!done);

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

lazyberezovskyCommented:
Again, I do not have any problems with encoding in this code.
What exactly causes encoding error (articleTiitle, caption, etc)?  How do you get those values?
0
Type25Author Commented:
It's articleCopy but i would presume it's the same for all of them.

ArticleCopy is the only text field that is multiline if that makes any difference.. ?

Retrieve the field like normal,  articleCopy.Text
0
japeteCommented:
Change the line:

                    TextWriter tw = new StreamWriter(articleLocation + htmlFilename, Encoding.UTF8);

Regards.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Type25Author Commented:
Spot on!
0
lazyberezovskyCommented:
Yes, that should work - File.CreateText do same thing about encoding.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.