I have a csv file with 2 rows in it.
A,1
B,2
I want to read this file in the format and convert it to the following format:
Name : A, ID: 1
Name : B, ID: 2
I am using the following .net api
using (FileStream reader = File.OpenRead(@"Data.csv")) // mind the encoding - UTF8
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.TrimWhiteSpace = true; // if you want
parser.Delimiters = new[] { "," };
parser.HasFieldsEnclosedInQuotes = true;
while (!parser.EndOfData)
{
string[] line = parser.ReadFields();
List<string> li = line.SelectMany(x => x.Split(',')).ToList();
}
}
In the li variable , I am able to get the individual elements after splitting for eg: A 1, but I am not getting how to add the key attribute to it, to make Name : A, ID : 1
Edit :
I need it in a list format finally as it is easy to separate the entities based on that eg:
List{
List1 = Name : A,ID:1
List2 = Name : B,ID:2
}
ReadFields();returns when (for example) you parse the first line?TextFieldParserclass if it gives you the exact result of aStreamReader.ReadLine()?