英文:
CsvHelper The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?)
问题
最初我的错误是没有以下部分:
var csvReaderConfig = new Configuration();
csvReaderConfig.HasHeaderRecord = false;
错误信息是 ArgumentException: 在 ExpandoObject 中已存在具有相同键 '' 的元素。 (参数 'key')
我被告知添加上述代码会解决此问题,然而,事实并非如此。我不确定是我在文档中漏掉了什么,还是某些东西没有安装。任何帮助将不胜感激,谢谢!
英文:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using System.IO;
using System.Globalization;
using CsvHelper.Configuration.Attributes;
using CsvHelper.Configuration;
namespace CsvHelperTest
{
internal class CsvHelperTester
{
static void Main(string[] args)
{
using (var streamReader = new StreamReader("C:\\Users\\eyoung\\Desktop\\parse test files\\SWR-150106-1_AN00045613_20150415_XF28572.csv"))
{
using (var csvReader = new CsvReader(streamReader, CultureInfo.InvariantCulture))
{
var csvReaderConfig = new Configuration();
csvReaderConfig.HasHeaderRecord = false;
var records = csvReader.GetRecords<dynamic>().ToList();
}
}
}
}
}
Initially my error without
var csvReaderConfig = new Configuration();
csvReaderConfig.HasHeaderRecord = false;
was ArgumentException: An element with the same key '' already exists in the ExpandoObject. (Parameter 'key')
and I was told adding the code above would solve this issue, however, it has not, not sure if I'm missing something in the documentation or if it's something that isn't installed, any help would be appreciated, thanks!
答案1
得分: 1
你需要使用 CsvConfiguration
,并将其传递给 CsvReader
构造函数。你可以在 入门页面 上找到使用 CsvConfiguration
的示例。
static void Main(string[] args)
{
var csvReaderConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
using (var streamReader = new StreamReader("C:\\Users\\eyoung\\Desktop\\parse test files\\SWR-150106-1_AN00045613_20150415_XF28572.csv"))
{
using (var csvReader = new CsvReader(streamReader, csvReaderConfig))
{
var records = csvReader.GetRecords<dynamic>().ToList();
}
}
}
英文:
You need to use CsvConfiguration
and it needs to be passed into the CsvReader
constructor. You can find examples of using CsvConfiguration
on the Getting Started Page
static void Main(string[] args)
{
var csvReaderConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
using (var streamReader = new StreamReader("C:\\Users\\eyoung\\Desktop\\parse test files\\SWR-150106-1_AN00045613_20150415_XF28572.csv"))
{
using (var csvReader = new CsvReader(streamReader, csvReaderConfig))
{
var records = csvReader.GetRecords<dynamic>().ToList();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论