C#,将字符添加到包含符号的字符串中

问题描述 投票:0回答:1

我通过c#将CSV文件转换为xml文件。我正在将csv文件保存在字符串列表中,但是它不使用ä,á,ê等符号。

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);
    }

将它们作为称为Lesson的对象返回。

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

阅读方法:

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));
    }

我该如何解决?

c# string char utf-16
1个回答
0
投票

感谢Cid。

问题是csv文件未另存为utf-8 csv文件。 Excel中有多个选项。如果您有相同的问题,请查看该帖子:How to check encoding of a CSV file

© www.soinside.com 2019 - 2024. All rights reserved.