Ismail
Ismail

Reputation: 993

Encoding issue with csvhelper and html markup

I am using csvhelper 15. My code looks like:

using (var reader = new StreamReader(_csvPath, Encoding.UTF8))
        {
            using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
            {
                csv.Configuration.MissingFieldFound = null;
                csv.Configuration.TrimOptions = TrimOptions.Trim;
                csv.Configuration.Encoding = Encoding.UTF8;

                profiles = csv.GetRecords<Profile>().ToList();
            }
        }

In one of my fields in the csv i have html markup looks like:

<p>Standard Chartered plc on its fully underwritten 2 for 7 rights issue to raise approximately £3.3 billion</p>

The pound sign ends up at ? so looks like encoding, I am using utf8 however still an issue on the read.

Am i missing something?

Upvotes: 0

Views: 1808

Answers (2)

Josh Close
Josh Close

Reputation: 23393

CsvHelper uses a TextReader and knows nothing about the encoding of the file. That is something that needs to be handled before passing the TextReader into `CsvHelper.

The CultureInfo that is required in the constructor is used for type conversions. When a string is converted into an int or DateTimeOffset, it uses the CultureInfo that is supplied.

In your case, the ? values are due to the encoding set on the TextReader that was supplied to CsvHelper.

Upvotes: 1

Ismail
Ismail

Reputation: 993

Ok updated my code to:

var westernEncoding = Encoding.GetEncoding("windows-1254");

        using (var reader = new StreamReader(_csvPath, westernEncoding))
        {
            using (var csv = new CsvReader(reader, new CultureInfo("en-GB")))
            {
                csv.Configuration.MissingFieldFound = null;
                csv.Configuration.TrimOptions = TrimOptions.Trim;
                csv.Configuration.Encoding = westernEncoding;

                profiles = csv.GetRecords<Profile>().ToList();
            }
        }

Now it works

Upvotes: 0

Related Questions