mdieod
mdieod

Reputation: 49

C# writing to file using UTF8

I have this simple code:

using (var sw = new StreamWriter(path + kat + "/" + namec, true, Encoding.UTF8))
{
    sw.WriteLine(namex + "," + address + "," + web + "," + email + "," + phone + "," + linksx + "," + transport);
}

I using Selenium Web Driver to download data from Denmark website, example link:http://www.visitdenmark.dk/da/danmark/danhostel-roenne-gdk614558

Then I store it in .csv file, but still I have characters like this Ă instead of this ø. As you can see I set Encoding.UTF8 Also it becoming more interesting when I set bool append to false, then everything is normal, but it does not help me because I need to append that file with new data. How can I fix this ?

Upvotes: 0

Views: 559

Answers (1)

Xiaoy312
Xiaoy312

Reputation: 14477

You should use windows-1252(or, Encoding.GetEncoding(1252)) instead of UTF-8, for Danish.


EDIT:

It was a missing BOM issue causing Excel unable to read the file properly.

// notice we are creating a new file here
using (var writer = new StreamWriter(path, false, Encoding.UTF8))
{
    // Add a BOM in the beginning of the file
    var preamble = Encoding.UTF8.GetPreamble();
    writer.BaseStream.Write(preamble, 0, preamble.Length);

    // write data...
    writer.WriteLine(string.Join(",", "Danhostel Rønne", "Arsenalvej 12 3700 Rønne"));
}

// when you write the same file again, you don't need to append the BOM again
using (var writer = new StreamWriter(path, true, Encoding.UTF8))
{
    // write more data...
    writer.WriteLine(string.Join(",", "Danhostel Rønne", "Arsenalvej 12 3700 Rønne"));
}

Upvotes: 1

Related Questions