CsvHelper The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?)

huangapple go评论91阅读模式
英文:

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(&quot;C:\\Users\\eyoung\\Desktop\\parse test files\\SWR-150106-1_AN00045613_20150415_XF28572.csv&quot;))
	{
		using (var csvReader = new CsvReader(streamReader, csvReaderConfig))
		{
			var records = csvReader.GetRecords&lt;dynamic&gt;().ToList();
		}
	}
}

huangapple
  • 本文由 发表于 2023年6月5日 23:57:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定