I want to replace accented characters (e.g. á, ñ, ¿, ¡, etc.) with the corresponding HTML codes (such as & aacute ;, & ntilde ;, & iquest ;, ie iecl ;, etc.) .
For example, this line of text:
The incomprehensible I ha-shido reshurme á las repetidas instancias que el Caballero Trelawney, el Doctor Livesey y otros muchos señores me
... should become:
The incomprehensible I ha-sid rehusarme á aacute á las repetidas instancias que el Caballero Trelawney, el Doctor Livesey y otros muchos señores me
It should be easy. I have this code to try:
private void buttonReplaceCharsWithCodes_Click(object sender, EventArgs e) { String fallName = String.Empty; List<String> linesModified = new List<string>(); StreamReader file = null; try // finally { try // catch { DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { fallName = openFileDialog1.FileName; } file = new StreamReader(fallName); String line; while ((line = file.ReadLine()) != null) { linesModified.Add(line); } progressBar1.Maximum = linesModified.Count; progressBar1.Value = 0; labelProgFeedback.Text = "Replacing accented chars with HTML codes"; for (int i = 0; i < linesModified.Count; i++) { linesModified[i] = linesModified[i].Replace("á", "á"); linesModified[i] = linesModified[i].Replace("Á", "Á"); linesModified[i] = linesModified[i].Replace("é", "é"); linesModified[i] = linesModified[i].Replace("É", "É"); linesModified[i] = linesModified[i].Replace("í", "í"); linesModified[i] = linesModified[i].Replace("Í", "Í"); linesModified[i] = linesModified[i].Replace("ñ", "ñ"); linesModified[i] = linesModified[i].Replace("Ñ", "Ñ"); linesModified[i] = linesModified[i].Replace("ó", "ó"); linesModified[i] = linesModified[i].Replace("Ó", "Ó"); linesModified[i] = linesModified[i].Replace("ú", "ú"); linesModified[i] = linesModified[i].Replace("Ú", "Ú"); linesModified[i] = linesModified[i].Replace("ü", "ü"); linesModified[i] = linesModified[i].Replace("Ü", "Ü"); linesModified[i] = linesModified[i].Replace("¿", "¿"); linesModified[i] = linesModified[i].Replace("¡", "¡"); progressBar1.PerformStep(); } progressBar1.Value = 0; } catch (Exception ex) { MessageBox.Show(String.Format("Exception {0}", ex.Message)); } } finally { String massagedFileName = String.Format("{0}_Massaged.txt", fallName); File.WriteAllLines(massagedFileName, linesModified); file.Close(); } }
Unfortunately, it does not work. Does it replace accented characters with what the hell? character () instead of the desired HTML code. What is required to make this work?
UPDATE
In response to comments, this is the contents of the uploaded file:
Incomprehensible, I am behind me, I love you, I love you! hecho para que escribiese la historia circunstanciada y completa de la Isla del Tesoro. Voy, pues, poner manos á la obra contándolo todo, desde el alfa hasta el omega, sin dejarme cosa alguna en el tintero, exceptuando la decinación geográfica de la isla, y esto tan solamente porque tengo por seguro que en eía exist to d no descubierto. Tomo la pluma en el año de gracia de 17 - y retrocedo hasta la época en que mi padre tenía aún la posada del "Almirante Benbow", y hasta el día en que por primera vez llegó á alojarse en ella aquel viejo marino de tez bronceada y curtida por los elementos, con su grande y visible cicatriz.
... and this is the file that it saves with a replacement:
Incomprehensible to me ha-sido reusurmir las repetidas instancias que el Caballero Trelawney, el Doctor Livesey y otros muchos sezores me han hecho para que escribiese la historia circunstanciada y completa de la Isla del Tesoro. Voy, pues, poner manos la obra cont ndolo todo, desde el alfa hasta el omega, sin dejarme cosa alguna en el tintero, exceptuando la defininaci n geogr fica de la isla, y esto tan solamente porque tengo por seguro que en ella existe todav a un tesoro no descubierto. Tomo la pluma en el ao de gracia de 17 - y retrocedo hasta la poca en que mi padre ten aan la posada del "Almirante Benbow", y hasta el da en que por primera vez lleg alojarse en ella aquel viejo marino de tez bronceada y curtida por los elementos, con su grande y visible cicatriz.
IOW, there is no replacement - I just see the "mystery" symbol instead of HTML codes.
I see the same thing at runtime when I look at the code and look at the individual linesModified lines (I see s). Better than seeing the stars, I think.
This is a process: it is a simple utility when I click a button to open a file (.txt). After processing, it saves the new version of the file to a new file.
UPDATE 2
Since you can explicitly save UTF8, I thought it might be useful to read the file, but this:
while ((line = file.ReadLine(ASCIIEncoding.UTF8)) != null)
... does not compile, saying that there is no overload of the ReadLine method, which takes 1 argument.