I am trying to use CsvHelper library in one of my projects. its just console application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using System.IO;
using Newtonsoft.Json;
using CsvHelper.TypeConversion;
namespace csvtest4
{
class Program
{
static void Main(string[] args)
{
List<string> result = new List<string>();
string value;
using (TextReader fileReader = File.OpenText("list.csv"))
{
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = true;
csv.Configuration.RegisterClassMap<CustomClassMap>();
while (csv.Read())
{
for (int i = 0; csv.TryGetField<string>(i, out value); i++)
{
result.Add(value);
}
}
var json = JsonConvert.SerializeObject(result);
Console.WriteLine(json.ToString());
}
}
}
public class CustomClassMap : CsvClassMap<TestModel>
{
public CustomClassMap()
{
Map(m => m.StringProperty).Index(0);
Map(m => m.GuidProperty).Index(1);
Map(m => m.IntProperty).Index(2).TypeConverter<MyCustomTypeConverter>();
}
}
}
Not sure, what I am missing but my vs2015 keeps complaining about CsvClassMap its saying type or namespace not found, are you missing directives. Also, it's not recognizing map function either
I think I added required namespace as other CsvHelper function working fine
CsvClassMap
is no longer in the latest versions of CsvHelper.
Use ClassMap<Model>
instead
CsvClassMap
is in namespace CsvHelper.Configuration
.
If you put your cursor on CsvClassMap
and hit Ctrl+.
, it should pull up a menu to resolve the missing reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With