0

Im converting csv files to xml files via c#. I'm saving the csv file in a List of string, but it doesn't take symbols symbols such as ä, á, ê.

public Lesson CsvToLesson(List<string> csv) 
        {
            string lesName = csv[0][csv[0].Length - 3].ToString();
            List<Word> words = new List<Word>();
        for(int i = 3; i < csv.Count; i++)
        {
            string lang1 = "";
            string lang2 = "";
            bool firstWord = true;
            foreach (char c in csv[i])
            {
                if (firstWord)
                {
                    if(c != ';')
                    {
                        lang1 += c;
                    } else
                    {
                        firstWord = false;
                    }
                } else {
                    if (c != ';')
                    {
                        lang2 += c;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            words.Add(new Word(lang1, lang2, 1, i));
        }
        return new Lesson(lesName, words);
    }

to return them as an Object called Lesson.

 <Word kasten="1" id="24">
    <lang1>eine Sekret�rin</lang1>
    <lang2>une secr�taire</lang2>
  </Word>

The reading method:

public void saveCsv(string path)
    {
        string line;
        List<string> csv = new List<string>();

        StreamReader file = new StreamReader(path);

        while((line = file.ReadLine()) != null)
        {
            csv.Add(line);
        }

        file.Close();
        AddLesson(controller.CsvToLesson(csv));
    }

How can I fix this?

EKnot
  • 151
  • 1
  • 9
  • 5
    It seems you're using the wrong encoding to either read or write the file. – germi Oct 24 '19 at 14:45
  • 2
    how are you reading the csv file? hope you are encoding with UTF8 – Krishna Varma Oct 24 '19 at 14:47
  • We need to see the previous part of the code, where you are reading the CSV – Cid Oct 24 '19 at 14:48
  • 2
    Another issue: It is inefficient to build a string like `lang1` or `lang2` in a loop with `+=`. Besides that, to find out if you read or write (or both!) with a wrong encoding, it would be helpful if you debugged and set a breakpoint in `CsvToLesson`. Do the entries in the `List csv` look correct when you debug (that is `"Sekretärin"`, `"Secrétaire"`), or are they already messed up there? In the latter case, you have an encoding issue when _reading_. – Jeppe Stig Nielsen Oct 24 '19 at 15:18
  • What is the encoding type of the CSV file ? – Cid Oct 25 '19 at 06:42

1 Answers1

0

Thanks to Cid.

The Problem was that the csv file wasn't saved as utf-8 csv file. There are multiple options in Excel. Take a look at that post, if you have the same problem: How to check encoding of a CSV file

EKnot
  • 151
  • 1
  • 9