C# 中的 TextFieldParser 问题
Issue with TextFieldParser in C#
我在 Windows 7 Enterprise 上使用 Visual Studio 2013、.NET 4.5.2、OpenXml 2.5。我创建了一段代码,将两个 .csv 文件导入到一对通用列表中,并比较它们的异常情况。当我去构建一个通用的 class 来加载 .csv 文件时,TextFieldParser 将无法工作。它适用于 Main(),但不适用于 class.
虽然我怀疑答案与 "using alias within a namespace" 有关,但它在 Main() 中完美运行。 Microsoft 有一个免责声明,即 rhs 不能是泛型类型,但如果它在一个地方有效,为什么另一个地方不行?
这是预期的行为还是有已知的解决方法?
背景:TextFieldParser 是一个 VB.NET class,可以巧妙地处理 .csv 或固定字段文件的解析。通用列表或 List 是可以处理其中任何内容的列表 classes。在我的例子中,我使用记录 class 作为包含的对象。 OpenXml 是一项 Microsoft 技术,用于与 Office 产品一起处理 XML 配置文件或连接字符串。我将导入配置设置以使 classes 使用通用。它正在尝试为我遇到问题的 TextFieldParser 构建通用 class 。 Google 已经让我走了这么远,但没有更远。
Main()中的工作代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;
public static void Main(string[] args)
{
// HARD_CODED FOR EXAMPLE ONLY - TO BE RETRIEVED FROM APP.CONFIG IN REAL PROGRAM
string hospPath = @"C:\events\inbound\OBLEN_COB_Active_Inv_Advi_Daily_.csv";
string vendPath = @"C:\events\outbound\Advi_OBlen_Active_Inv_Ack_Daily_.csv";
List<DenialRecord> hospList = new List<DenialRecord>();
List<DenialRecord> vendList = new List<DenialRecord>();
using (TextFieldParser hospParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(hospPath))
{
hospParser.TextFieldType = FieldType.Delimited;
hospParser.SetDelimiters(",");
hospParser.HasFieldsEnclosedInQuotes = false;
hospParser.TrimWhiteSpace = true;
while (!hospParser.EndOfData)
{
try
{
string[] row = hospParser.ReadFields();
if (row.Length <= 7)
{
DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
hospList.Add(dr);
}
}
catch (Exception e)
{
Console.WriteLine("Error is: {0}", e.ToString());
}
}
hospParser.Close();
hospParser.Dispose();
}
class 中的非工作代码本身:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;
namespace InputSide
{
internal class CsvLoader
{
private int columnCount = 7; // backing store
private string filePath;
private List<DenialRecord> inputList = new List<DenialRecord>();
public List<DenialRecord> InputList
{
set
{
this.inputList = InputList;
}
get
{
return inputList;
}
}
// Default constructor
internal CsvLoader()
{
}
// Exposed constructor
public CsvLoader(List<DenialRecord> InputList, string FilePath, int ColumnCount)
{
this.inputList = InputList;
this.filePath = FilePath;
}
// Load csv to List<T>
public List<DenialRecord> CsvList(int columnCount)
{
private int columnCount;
using (TextFieldParser csvParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePath))
{
csvParser.TextFieldType = FieldType.Delimited;
csvParser.SetDelimiters(",");
csvParser.HasFieldsEnclosedInQuotes = false;
csvParser.TrimWhiteSpace = true;
while (!csvParser.EndOfData)
{
try
{
string[] row = csvParser.ReadFields();
if (row.Length <= 7)
{
DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
inputList.Add(dr);
}
}
catch (Exception e)
{
// do something
Console.WriteLine("Error is: {0}", e.ToString());
}
}
csvParser.Close();
csvParser.Dispose();
}
return inputList;
}
}
}
编辑:
添加的错误列表:
Error 1 } expected c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 43 10 InputSide
Error 2 Invalid token 'using' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 46 13 InputSide
Error 3 ; expected c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 46 77 InputSide
Error 4 Invalid token '=' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 48 41 InputSide
Error 5 Invalid token ';' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 48 62 InputSide
Error 6 Invalid token '(' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 49 40 InputSide
Error 7 Invalid token '=' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 50 53 InputSide
Error 8 Invalid token '=' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 51 42 InputSide
Error 9 Invalid token ')' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 53 44 InputSide
Error 10 Invalid token 'if' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 59 25 InputSide
Error 11 Invalid token '<=' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 59 40 InputSide
Error 12 Invalid token '(' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 62 42 InputSide
Error 13 Invalid token ')' in class, struct, or interface member
declaration c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 62 45 InputSide
Error 14 A namespace cannot directly contain members such as fields or
methods c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 65 21 InputSide
Error 15 Type or namespace definition, or end-of-file
expected c:\users\jas189\documents\visual studio
2013\Projects\InputSide\InputSide\CsvLoader.cs 70 17 InputSide
Error 16 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.filePath' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 46 68 InputSide
Error 17 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.csvParser' c:\users\jas189\documents\visual
studio 2013\projects\inputside\inputside\csvloader.cs 58 40 InputSide
Error 18 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 61 64 InputSide
Error 19 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 61 72 InputSide
Error 20 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 61 80 InputSide
Error 21 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 61 88 InputSide
Error 22 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 61 96 InputSide
Error 23 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 61 104 InputSide
Error 24 A field initializer cannot reference the non-static field,
method, or property
'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 61 112 InputSide
Error 25 'InputSide.CsvLoader.CsvList(int)': not all code paths return
a value c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 42 35 InputSide
Error 26 'InputSide.CsvLoader.csvParser' is a 'field' but is used like
a 'type' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 48 17 InputSide
Error 27 'Microsoft.VisualBasic.FileIO.FieldType.Delimited' is a
'field' but is used like a 'type' c:\users\jas189\documents\visual
studio 2013\projects\inputside\inputside\csvloader.cs 48 53 InputSide
Error 28 'InputSide.CsvLoader.csvParser' is a 'field' but is used like
a 'type' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 49 17 InputSide
Error 29 'InputSide.CsvLoader.csvParser' is a 'field' but is used like
a 'type' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 50 17 InputSide
Error 30 'InputSide.CsvLoader.csvParser' is a 'field' but is used like
a 'type' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 51 17 InputSide
Error 31 'InputSide.CsvLoader.csvParser' is a 'field' but is used like
a 'type' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 53 25 InputSide
Error 32 'InputSide.CsvLoader.row' is a 'field' but is used like a
'type' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 59 29 InputSide
Error 33 'InputSide.CsvLoader.inputList' is a 'field' but is used like
a 'type' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 62 29 InputSide
Error 34 'InputSide.CsvLoader.dr' is a 'field' but is used like a
'type' c:\users\jas189\documents\visual studio
2013\projects\inputside\inputside\csvloader.cs 62 43 InputSide
最终工作代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;
namespace InputSide
{
internal class CsvLoader
{
private int columnCount = 7; // backing store
private string filePath;
private List<DenialRecord> inputList = new List<DenialRecord>();
public List<DenialRecord> InputList
{
set
{
this.inputList = InputList;
}
get
{
return inputList;
}
}
// Default constructor
internal CsvLoader()
{
}
// Exposed constructor
public CsvLoader(List<DenialRecord> InputList, string FilePath, int ColumnCount)
{
this.inputList = InputList;
this.filePath = FilePath;
}
// Load csv to List<T>
public List<DenialRecord> CsvList(int columnCount)
{
using (TextFieldParser csvParser = new TextFieldParser(filePath))
{
csvParser.TextFieldType = FieldType.Delimited;
csvParser.SetDelimiters(",");
csvParser.HasFieldsEnclosedInQuotes = false;
csvParser.TrimWhiteSpace = true;
while (!csvParser.EndOfData)
{
try
{
string[] row = csvParser.ReadFields();
if (row.Length <= 7)
{
DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
inputList.Add(dr);
}
}
catch (Exception e)
{
// do something
Console.WriteLine("Error is: {0}", e.ToString());
}
}
csvParser.Close();
csvParser.Dispose();
}
return inputList;
}
}
}
除了可能缺少对 Microsoft.VisualBasic 的引用之外:
在CsvList
- 您不能声明
private int columnCount;
因为该名称已用作参数并且
- 您应该使用
if (row.Length == 7)
,因为如果元素少于七个,则DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
将失败并在[=处出现"Index out of bounds"错误28=]-时间.
虽然我怀疑答案与 "using alias within a namespace" 有关,但它在 Main() 中完美运行。 Microsoft 有一个免责声明,即 rhs 不能是泛型类型,但如果它在一个地方有效,为什么另一个地方不行?
这是预期的行为还是有已知的解决方法?
背景:TextFieldParser 是一个 VB.NET class,可以巧妙地处理 .csv 或固定字段文件的解析。通用列表或 List 是可以处理其中任何内容的列表 classes。在我的例子中,我使用记录 class 作为包含的对象。 OpenXml 是一项 Microsoft 技术,用于与 Office 产品一起处理 XML 配置文件或连接字符串。我将导入配置设置以使 classes 使用通用。它正在尝试为我遇到问题的 TextFieldParser 构建通用 class 。 Google 已经让我走了这么远,但没有更远。
Main()中的工作代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;
public static void Main(string[] args)
{
// HARD_CODED FOR EXAMPLE ONLY - TO BE RETRIEVED FROM APP.CONFIG IN REAL PROGRAM
string hospPath = @"C:\events\inbound\OBLEN_COB_Active_Inv_Advi_Daily_.csv";
string vendPath = @"C:\events\outbound\Advi_OBlen_Active_Inv_Ack_Daily_.csv";
List<DenialRecord> hospList = new List<DenialRecord>();
List<DenialRecord> vendList = new List<DenialRecord>();
using (TextFieldParser hospParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(hospPath))
{
hospParser.TextFieldType = FieldType.Delimited;
hospParser.SetDelimiters(",");
hospParser.HasFieldsEnclosedInQuotes = false;
hospParser.TrimWhiteSpace = true;
while (!hospParser.EndOfData)
{
try
{
string[] row = hospParser.ReadFields();
if (row.Length <= 7)
{
DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
hospList.Add(dr);
}
}
catch (Exception e)
{
Console.WriteLine("Error is: {0}", e.ToString());
}
}
hospParser.Close();
hospParser.Dispose();
}
class 中的非工作代码本身:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;
namespace InputSide
{
internal class CsvLoader
{
private int columnCount = 7; // backing store
private string filePath;
private List<DenialRecord> inputList = new List<DenialRecord>();
public List<DenialRecord> InputList
{
set
{
this.inputList = InputList;
}
get
{
return inputList;
}
}
// Default constructor
internal CsvLoader()
{
}
// Exposed constructor
public CsvLoader(List<DenialRecord> InputList, string FilePath, int ColumnCount)
{
this.inputList = InputList;
this.filePath = FilePath;
}
// Load csv to List<T>
public List<DenialRecord> CsvList(int columnCount)
{
private int columnCount;
using (TextFieldParser csvParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePath))
{
csvParser.TextFieldType = FieldType.Delimited;
csvParser.SetDelimiters(",");
csvParser.HasFieldsEnclosedInQuotes = false;
csvParser.TrimWhiteSpace = true;
while (!csvParser.EndOfData)
{
try
{
string[] row = csvParser.ReadFields();
if (row.Length <= 7)
{
DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
inputList.Add(dr);
}
}
catch (Exception e)
{
// do something
Console.WriteLine("Error is: {0}", e.ToString());
}
}
csvParser.Close();
csvParser.Dispose();
}
return inputList;
}
}
}
编辑:
添加的错误列表:
Error 1 } expected c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 43 10 InputSide
Error 2 Invalid token 'using' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 46 13 InputSide
Error 3 ; expected c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 46 77 InputSide
Error 4 Invalid token '=' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 48 41 InputSide
Error 5 Invalid token ';' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 48 62 InputSide
Error 6 Invalid token '(' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 49 40 InputSide
Error 7 Invalid token '=' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 50 53 InputSide
Error 8 Invalid token '=' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 51 42 InputSide
Error 9 Invalid token ')' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 53 44 InputSide
Error 10 Invalid token 'if' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 59 25 InputSide
Error 11 Invalid token '<=' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 59 40 InputSide
Error 12 Invalid token '(' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 62 42 InputSide
Error 13 Invalid token ')' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 62 45 InputSide
Error 14 A namespace cannot directly contain members such as fields or methods c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 65 21 InputSide
Error 15 Type or namespace definition, or end-of-file expected c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 70 17 InputSide
Error 16 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.filePath' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 46 68 InputSide
Error 17 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.csvParser' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 58 40 InputSide Error 18 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 64 InputSide
Error 19 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 72 InputSide
Error 20 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 80 InputSide
Error 21 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 88 InputSide
Error 22 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 96 InputSide
Error 23 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 104 InputSide
Error 24 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 112 InputSide
Error 25 'InputSide.CsvLoader.CsvList(int)': not all code paths return a value c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 42 35 InputSide
Error 26 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 48 17 InputSide
Error 27 'Microsoft.VisualBasic.FileIO.FieldType.Delimited' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 48 53 InputSide Error 28 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 49 17 InputSide
Error 29 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 50 17 InputSide
Error 30 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 51 17 InputSide
Error 31 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 53 25 InputSide
Error 32 'InputSide.CsvLoader.row' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 59 29 InputSide
Error 33 'InputSide.CsvLoader.inputList' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 62 29 InputSide
Error 34 'InputSide.CsvLoader.dr' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 62 43 InputSide
最终工作代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;
namespace InputSide
{
internal class CsvLoader
{
private int columnCount = 7; // backing store
private string filePath;
private List<DenialRecord> inputList = new List<DenialRecord>();
public List<DenialRecord> InputList
{
set
{
this.inputList = InputList;
}
get
{
return inputList;
}
}
// Default constructor
internal CsvLoader()
{
}
// Exposed constructor
public CsvLoader(List<DenialRecord> InputList, string FilePath, int ColumnCount)
{
this.inputList = InputList;
this.filePath = FilePath;
}
// Load csv to List<T>
public List<DenialRecord> CsvList(int columnCount)
{
using (TextFieldParser csvParser = new TextFieldParser(filePath))
{
csvParser.TextFieldType = FieldType.Delimited;
csvParser.SetDelimiters(",");
csvParser.HasFieldsEnclosedInQuotes = false;
csvParser.TrimWhiteSpace = true;
while (!csvParser.EndOfData)
{
try
{
string[] row = csvParser.ReadFields();
if (row.Length <= 7)
{
DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
inputList.Add(dr);
}
}
catch (Exception e)
{
// do something
Console.WriteLine("Error is: {0}", e.ToString());
}
}
csvParser.Close();
csvParser.Dispose();
}
return inputList;
}
}
}
除了可能缺少对 Microsoft.VisualBasic 的引用之外:
在CsvList
- 您不能声明
private int columnCount;
因为该名称已用作参数并且 - 您应该使用
if (row.Length == 7)
,因为如果元素少于七个,则DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
将失败并在[=处出现"Index out of bounds"错误28=]-时间.