Special characters and Encoding text in C#?

I've been programming an application to read some of the information from my personal web page, I know it's not the best code out there. unfortunatly it has NOT worked for me when i get special characters like the ones used in spanish or french; á, é, í, ó, ú and others

"más percusión"

comes out as:

- "más percusión"

So when I save some of the info in a DB it saves it with weird chars or a "?" simbol, not the correct ones.

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

I tried using the line:

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


 It started to work, But i dont know why.  Now I get just a "?" simbol instead of the right char.

Thanks for the help!
public string getDocumento_Txtx(string direccion)
        {
            System.Net.WebClient Client = new WebClient();
            Stream strm = null;
            try
            {
                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 = "";
            do
            {

                line = sr.ReadLine();


                todo_1 = todo_1 + Environment.NewLine + line;

            }
            while (line != null);

            strm.Close();

            return todo_1;

        }

Open in new window

LVL 1
rperezretanaAsked:
Who is Participating?
 
DanielD2004Commented:
Try UTF8.
0
 
rperezretanaAuthor Commented:
I already saved the content in a text file, but.. same, it saves the "?" chars like:

"más percusión"

comes out as:

- "m?s percusi?n"
0
 
rperezretanaAuthor Commented:
Actually that one was verry good!!...

I used this code and it worked ;)
StreamReader sr = new StreamReader(strm,Encoding.UTF8);

Open in new window

0
 
rperezretanaAuthor Commented:
Thanks for help:
I used like this:


StreamReader sr = new StreamReader(strm,Encoding.UTF8);
 and now its working
0
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.

All Courses

From novice to tech pro — start learning today.