Special characters and formated text in C#?

I am programming an aplication reading some information from my own web page, I know is not the best code ever done, but this is what I am using, and when I get some special characters like in spanish or french á, é, í, ó, ú or others, so when it is reading a phrase like:

- "más percusión"

It returns like:

- "más percusión"

So when I save some of the info in a DB it save me that with weird chars, not the normal ones.

I am using the Encoding 1252, Any idea to fix the problem?

public string getDocumento_Txtx(string direccion)
            System.Net.WebClient Client = new WebClient();
            Stream strm = null;
                strm = Client.OpenRead(direccion);
            }catch(Exception r)
                if (r.Message.Contains("404"))
                    return "Archivo Inexistente";

            StreamReader sr = new StreamReader(strm,    System.Text.Encoding.GetEncoding(1252));
            string line = "";
            string todo_1 = "";

                line = sr.ReadLine();

                todo_1 = todo_1 + Environment.NewLine + line;

            while (line != null);


            return todo_1;


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

rperezretanaAuthor Commented:
Ok, I found a solution, and this sucks... I paid so much for this answer and someone in ayhoo answers gave me a clue...

I just change the return value for:

return System.Text.Encoding.GetEncoding(1252).GetString(Encoding.GetEncoding(1252).GetBytes(todo_1.ToCharArray()));

And that solved my problem...
return System.Text.Encoding.GetEncoding(1252).GetString(Encoding.GetEncoding(1252).GetBytes(todo_1.ToCharArray()));

Open in new window


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
I wonder if you will have to precede your string value with N while storing it in database. i .e. while specifying it as a parameter to your stored proc or even directly in the insert query.

You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server

Check this out link as well.

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
Editors IDEs

From novice to tech pro — start learning today.