2

I'm reading from a CSV file where the first line is the header row. All column names are read and put into a table like this:

foreach (var item in lines[0].Split(';').Where(s => !string.IsNullOrEmpty(s)))
        {
            table.Columns.Add(item.Trim(), typeof(string));
        }

Everything seams correct, except when I try to read from the table again. Most of the columns are read, except one.

I have tried to debug and write a message for this column, but it does NOT enter into that function (see image below). (The if statement return false)

enter image description here

I have tried about everything now, including checking the Locale on the table.

Header line:

document title;DocRef;DocRevNo;DocRevDt;RevisionObject;TransRef;Trans Status;DocOrigin;DocRefClient;RespActualDt;First TransDate;Last Update

EDIT:

I have found out that first byte is a strange one...

[0]: 65279 ''
[1]: 100 'd'
[2]: 111 'o'
[3]: 99 'c'
[4]: 117 'u'
[5]: 109 'm'
[6]: 101 'e'
[7]: 110 'n'
[8]: 116 't'
[9]: 32 ' '
[10]: 116 't'
[11]: 105 'i'
[12]: 116 't'
[13]: 108 'l'
[14]: 101 'e'

Anyone that knows how to remove that byte (If it exist)?

thomas
  • 1,399
  • 1
  • 17
  • 32
  • could you just add your first line from csv ? – mukesh kudi Oct 12 '18 at 08:26
  • I have repeated your code in LinqPad. Everything works for me. Have you tried to look at every ColumnName? I mean is the column exist? Sometimes I met this bug. But in my case, it was one letter but in different languages. F.e. letter 'c' on English and 'с' on Russian looks same, but it differences letters. – Dmitresky Oct 12 '18 at 13:38
  • Very good point. The source is from France, I'm on a norwegian culture... Initial tests on that did not solve it, but I will investigate more... – thomas Oct 13 '18 at 16:08

1 Answers1

0

Here is the solution. The start of the line contained a hidden charakter (65279). By running the text through this function, it removed the problem:

    protected string CleanInput(string input)
    {
        var imp = (input ?? "");
        var removeChars = new Char[] { (Char)65279 };
        imp = new String(imp.ToCharArray().Where(c => !removeChars.Contains(c)).ToArray());
        return imp;
    }
thomas
  • 1,399
  • 1
  • 17
  • 32