I use CsvHelper 4.0.3.
I have a nested class defined like this:
private class CsvLine {
public string Solution;
public string Project;
public string DependsOnProject;
public string Weight;
public string DependsOnPackage;
public string PackageVersion;
}
My .csv file, which I want to parse using CsvHelper
, has these field names:
Solution,Project,DependsOnProject,Weight,DependsOnPackage,PackageVersion
Here is my code:
TextReader readFile = new StreamReader(dependenciesCsvFilePath);
var csvReader = new CsvReader(readFile);
IEnumerable<CsvLine> records = csvReader.GetRecords<CsvLine>();
According to the documentation here, the code above should work.
However, when I inspect records
, I see the message No members are mapped for type 'ParentClass+CsvLine'
.
I changed the accessibility of CsvLine
from private
to public
, but that made no difference.
What did I do wrong?
EDIT: I have tried un-nesting the CsvLine
class and making it public, but that did not help either.
EDIT: I made the changes as Nkosi suggested; however, now it says that records
is an empty collection: "Enumeration yielded no results". There are definitely data present inside my .csv file, so the collection shouldn't be empty.
Here are some sample data:
Solution,Project,DependsOnProject,Weight,DependsOnPackage,PackageVersion
FOD.sln,ABC.DEF,IMS.ABC,1,,
FOD.sln,ABC.DEF,IMS.DEF,1,,
FOD.sln,ABC.DEF,IMS.GHI,1,,
FOD.sln,ABC.DEF,IMS.JKL,1,,
EDIT: Solved! Nkosi's and Panagiotis' answers complement each other's.
You need to use Properties instead of fields in the object model as by default, it will map the matching public members
public class CsvLine {
public string Solution { get; set; }
public string Project { get; set; }
public string DependsOnProject { get; set; }
public string Weight { get; set; }
public string DependsOnPackage { get; set; }
public string PackageVersion { get; set; }
}
You should also read up on mapping your classes to the csv file.