Link to home
Start Free TrialLog in
Avatar of Type25
Type25

asked on

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?  
Avatar of lazyberezovsky
lazyberezovsky
Flag of Belarus image

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?
Avatar of Carl Tawn
Can you show the code you are using to write to file? It sounds like a problem with the Encoding.
Avatar of Type25
Type25

ASKER

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

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?
Avatar of Type25

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of japete
japete

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Type25

ASKER

Spot on!
Yes, that should work - File.CreateText do same thing about encoding.