-2

i want to save the result of my List to be save as csv into a variable path with encoding UTF.

But when i loop with foreach over my list:

    foreach (String item in finalausgabe)
            {
                Console.WriteLine(ausgabe.GetType());
            }

i get the following result in my Console (just the first five lines for preview)

System.Collections.Generic.List1[System.String]
System.Collections.Generic.List1[System.String]
System.Collections.Generic.List1[System.String]
System.Collections.Generic.List1[System.String]
System.Collections.Generic.List1[System.String]

But my list looks actually like this:

"STATUS" "USERID" "Username" "Vorname" "Nachname" "Zweiter Vorname"
"active" "1" "testm" "test" "M" "" "M" "" "Kuhnkies" "active" "10" "lext" Sofia Daasch "" "F" "" "Lex" "active" "102" "reiterr" Anna Dabbagh "" "M" "" "Reiter" "active" "103" "buchmeiera" Lea Dabbert "" "M" "" "Buchmeier" "active" "104" "fuchss" Emilia Dabels "" "M" ""

as you can see its already prepared as a tab separated csv, just needs to be saved line by line in csv-file with encoding "utf-8".

June7
  • 19,874
  • 8
  • 24
  • 34
kb9992
  • 1

2 Answers2

0

well obviously you should write something like

foreach (String item in finalausgabe)
            {
                Console.WriteLine(item);
            }
0

I suggest using CsvHelper, and order your data in a insetead of making your data as separate strings, for example:

class MyRecord
{
    public string Status {get; set;}
    public string UserId {get; set;}
    public string UserName {get; set;}
}

then use the CsvHelper as the following:

List<MyRecord> records = yourFilledList;
var textWriter = new StreamWriter("your file path",isAppend, Encoding.UTF8);
var csv = new CsvWriter( textWriter );
csv.WriteRecords( records );

and as for the foreach the item will be available for you automatically in each iteration so you should use it directly, it follows

Console.WriteLine(element);

for more info about CsvHelper and how to install nuget check this.

Ali Ezzat Odeh
  • 2,093
  • 1
  • 17
  • 17