LINQ to CSV library
来源:互联网 发布:js 对象类型数组 编辑:程序博客网 时间:2024/05/29 10:55
12. 九月 2011 22:39
- Download source and sample code
- NuGet package
Contents
- Introduction
- Requirements
- Installation
- Quick Start
- Write Overloads
- Read Overloads
- Reading Raw Data Rows
- Deferred Reading
- CsvFileDescription
- CsvColumn Attribute
- Error Handling
Introduction
This library makes it easy to use CSV files with LINQ queries. Its features include:
- Follows the most common rules for CSV files. Correctly handles data fields that contain commas and line breaks.
- In addition to comma, most delimiting characters can be used, including tab for tab delimited fields.
- Can be used with an
IEnumarable
of an anonymous class - which is often returned by a LINQ query. - Supports deferred reading.
- Supports processing files with international date and number formats.
- Supports different character encodings if you need them.
- Recognizes a wide variety of date and number formats when reading files.
- Provides fine control of date and number formats when writing files.
- Robust error handling, allowing you to quickly find and fix problems in large input files.
Requirements
- To compile the library, you need a C# 2008 compiler, such as Visual Studio 2008 or Visual C# 2008 Express Edition.
- To run the library code, you need to have the .NET 3.5 framework installed.
Installation
These instructions apply to the download with sources and sample code. The NuGet package installs itself.
- Download the zip file with the source code, and unzip in a directory.
- Open the Code\LINQtoCSV.sln file in Visual Studio.
- You'll find that the sources are organized in a solution, with these elements:
- Project LINQtoCSV is the actual library.
- Project SampleCode has the sample code shown in this article.
- Project TestConsoleApplication is a working console application that exercises most of the features of the library. The code is heavily documented.
- The directory TestFiles within the TestConsoleApplication project contains test files - both CSV and tab delimited, and with both US and international (Dutch) dates and numbers.
- Compile the solution. This will produce a LINQtoCSV.dll file in the Code\LINQtoCSV\bin directory. You'll need that file to use the library in your own projects.
Quick Start
Reading from a file
- In your project, add a reference to the LINQtoCSV.dll you generated duringInstallation.
- The file will be read into an
IEnumerable<T>
, whereT
is a data class that you define. The data records read from the file will be stored in objects of this data class. You could define a data class along these lines:
using LINQtoCSV;using System;class Product{ [CsvColumn(Name = "ProductName", FieldIndex = 1)] public string Name { get; set; } [CsvColumn(FieldIndex = 2, OutputFormat = "dd MMM HH:mm:ss")] public DateTime LaunchDate { get; set; } [CsvColumn(FieldIndex = 3, CanBeNull = false, OutputFormat = "C")] public decimal Price { get; set; } [CsvColumn(FieldIndex = 4)] public string Country { get; set; } [CsvColumn(FieldIndex = 5)] public string Description { get; set; }}
With this definition, you could read into an IEnumerable<Product>
.
Although this example only uses properties, the library methods will recognize simple fields as well. Just make sure your fields/properties are public.
The optional CsvColumn
attribute allows you to specify whether a field/property is required, how it should be written to an output file, etc. Full details are availablehere.
- Import the
LINQtoCSV
namespace at the top of the source file where you'll be reading the file:
using LINQtoCSV;
- Create a
CsvFileDescription
object, and initialize it with details about the file that you're going to read. It will look like this:
CsvFileDescription inputFileDescription = new CsvFileDescription{ SeparatorChar = ',', FirstLineHasColumnNames = true};
This allows you to specify what character is used to separate data fields (comma, tab, etc.), whether the first record in the file holds column names, and a lot more (full details).
- Create a
CsvContext
object:
CsvContext cc = new CsvContext();
It is this object that exposes the Read
and Write
methods you'll use to read and write files.
- Read the file into an
IEnumerable<T>
using theCsvContext
object'sRead
method, like this:
IEnumerable<Product> products = cc.Read<Product>("products.csv", inputFileDescription);
This reads the file products.csv into the variable products
, which is of typeIEnumerable<Product>
.
- You can now access
products
via a LINQ query, aforeach
loop, etc.:
var productsByName = from p in products orderby p.Name select new { p.Name, p.LaunchDate, p.Price, p.Description };// or ...foreach (Product item in products) { .... }
To make it easier to get an overview, here is the code again that reads from a file, but now in one go:
CsvFileDescription inputFileDescription = new CsvFileDescription{ SeparatorChar = ',', FirstLineHasColumnNames = true};CsvContext cc = new CsvContext();IEnumerable<Product> products = cc.Read<Product>("products.csv", inputFileDescription);// Data is now available via variable products.var productsByName = from p in products orderby p.Name select new { p.Name, p.LaunchDate, p.Price, p.Description };// or ...foreach (Product item in products) { .... }
You'll find this same code in the SampleCode project in the sources.
Writing to a file
This is very similar to reading a file.
- In your project, add a reference to LINQtoCSV.dll.
- The
Write
method takes aIEnumerable<T>
and writes each object of typeT
in theIEnumerable<T>
as a data record to the file. The definition of your data class could look like this:
using LINQtoCSV;using System;class Product{ [CsvColumn(Name = "ProductName", FieldIndex = 1)] public string Name { get; set; } [CsvColumn(FieldIndex = 2, OutputFormat = "dd MMM HH:mm:ss")] public DateTime LaunchDate { get; set; } [CsvColumn(FieldIndex = 3, CanBeNull = false, OutputFormat = "C")] public decimal Price { get; set; } [CsvColumn(FieldIndex = 4)] public string Country { get; set; } [CsvColumn(FieldIndex = 5)] public string Description { get; set; }}
The optional CsvColumn attribute allows you to specify such things as what date and number formats to use when writing each data field. Details for all CsvColumn properties (CanBeNull
,OutputFormat
, etc.) are availablehere.
Although this example only uses properties, you can also use simple fields.
The Write
method will happily use an anonymous type for T
, so you can write the output of a LINQ query right to a file. In that case, you obviously won't defineT
yourself.Later on, you'll see an example of this.
- Import the
LINQtoCSV
namespace at the top of the source file where you'll be writing the file:
using LINQtoCSV;
- Make sure the data is stored in an object that implements
IEnumerable<T>
, such as aList<T>
, or theIEnumerable<T>
returned by theRead
method.
List<Product> products2 = new List<Product>();// Fill the list with products// ...
- Create a CsvFileDescription object, and initialize it with details about the file you will be writing, along these lines:
CsvFileDescription outputFileDescription = new CsvFileDescription{ SeparatorChar = '\t', // tab delimited FirstLineHasColumnNames = false, // no column names in first record FileCultureName = "nl-NL" // use formats used in The Netherlands};
- Create a
CsvContext
object:
CsvContext cc = new CsvContext();
- Invoke the
Write
method exposed by theCsvContext
object to write the contents of yourIEnumerable<T>
to a file:
cc.Write( products2, "products2.csv", outputFileDescription);
This writes the Product
objects in the variable products2
to the file "products2.csv".
Here is the code again that writes a file, but now in one go:
List<Product> products2 = new List<Product>();// Fill the list with products// ...CsvFileDescription outputFileDescription = new CsvFileDescription{ SeparatorChar = '\t', // tab delimited FirstLineHasColumnNames = false, // no column names in first record FileCultureName = "nl-NL" // use formats used in The Netherlands};CsvContext cc = new CsvContext();cc.Write( products2, "products2.csv", outputFileDescription);
Writing an IEnumerable of anonymous type
If you have a LINQ query producing an IEnumerable
of anonymous type, writing thatIEnumerable
to a file is no problem:
CsvFileDescription outputFileDescription = new CsvFileDescription{.....};CsvContext cc = new CsvContext();// LINQ query returning IEnumerable of anonymous type// into productsNetherlandsvar productsNetherlands = from p in products where p.Country == "Netherlands" select new { p.Name, p.LaunchDate, p.Price, p.Description };// Write contents of productsNetherlands to filecc.Write( productsNetherlands, "products-Netherlands.csv", outputFileDescription);
Here, a LINQ query selects all products for "Netherlands" from the variable products
, and returns an IEnumerable
holding objects of some anonymous type that has the fieldsName
,LaunchDate
,Price
, and Description
. TheWrite
method then writes those objects to the fileproducts-Netherlands.csv.
CsvContext.Write Overloads
Write<T>(IEnumerable<T> values, string fileName)
Write<T>(IEnumerable<T> values, string fileName, CsvFileDescription fileDescription)
Write<T>(IEnumerable<T> values, TextWriter stream)
Write<T>(IEnumerable<T> values, TextWriter stream, CsvFileDescription fileDescription)
Some interesting facts about these overloads:
- None of the overloads return a value.
- Unlike the
Read
method,Write
does not require thatT
has a parameterless constructor. - Overloads that take a stream write the data to the stream. Those that take a file name write the data to the file.
- Overloads that do not take a CsvFileDescription object simply create one themselves, using the default values for the
CsvFileDescription
properties.
CsvContext.Read Overloads
Read<T>(string fileName)
Read<T>(string fileName, CsvFileDescription fileDescription)
Read<T>(StreamReader stream)
Read<T>(StreamReader stream, CsvFileDescription fileDescription)
Some interesting facts about these overloads:
- Each overload returns an
IEnumerable<T>
. T
must have a parameterless constructor. If you do not define a constructor forT
, the compiler will generate a parameterless constructor for you.- Overloads that take a stream read the data from the stream. Those that take a file name read the data from the file. However, see the section ondeferred reading.
- Overloads that do not take a CsvFileDescription object simply create one themselves, using the default values for the
CsvFileDescription
properties.
Reading Raw Data Rows
Sometimes it's easier to read the raw data fields from the CSV file, instead of having them processed into objects by the library. For example if different rows can have different formats, or if you don't know at compile time which field is going to hold what data.
You can make this happen by having your type T implement the interface IDataRow
. This interface is included in the library, so you don't have to write it yourself. It essentially just describes a collection ofDataRowItem
objects:
The DataRowItem
class is also defined in the library. It describes each individual field within a data row:
public interface IDataRow{ // Number of data row items in the row. int Count { get; } // Clear the collection of data row items. void Clear(); // Add a data row item to the collection. void Add(DataRowItem item); // Allows you to access each data row item with an array index, such as // row[i] DataRowItem this[int index] { get; set; }}
public class DataRowItem{ ... // Line number of the field public int LineNbr { get { ... } } // Value of the field public string Value { get { ... } }}
public class DataRowItem{ ... // Line number of the field public int LineNbr { get { ... } } // Value of the field public string Value { get { ... } }}
The line number is included in the DataRowItem
class, because data rows can span multiple lines.
The easiest way to create a class that implements IDataRow
is to derive it fromList<DataRowItem>
:
using LINQtoCSV;internal class MyDataRow : List<DataRowItem>, IDataRow{}
Now you can read the CSV file into a collection of MyDataRow
objects:
IEnumerable<MyDataRow> products = cc.Read<MyDataRow>("products.csv", inputFileDescription);
You can then access each individual field within each data row:
foreach (MyDataRow dataRow in products){ string firstFieldValue = dataRow[0].Value; int firstFieldLineNbr = dataRow[0].LineNbr; string secondFieldValue = dataRow[1].Value; int secondFieldLineNbr = dataRow[1].LineNbr; ...}
Deferred Reading
Here is how the Read
overloads implement deferred reading:
- When you invoke the
Read
method (which returns anIEnumerable<T>
), no data is read yet. If using a file, the file is not yet opened. - When the Enumerator is retrieved from the
IEnumerable<T>
(for example, when starting aforeach
loop), the file is opened for reading. If using a stream, the stream is rewound (seek to start of the stream). - Each time you retrieve a new object from the Enumerator (for example, while looping through a
foreach
), a new record is read from the file or stream. - When you close the Enumerator (for example, when a
foreach
ends or when you break out of it), the file is closed. If using a stream, the stream is left unchanged.
This means that:
- If reading from a file, the file will be open for reading while you're accessing the
IEnumerable<T>
in aforeach
loop. - The file can be updated in between accesses. You could access the
IEnumerable<T>
in aforeach
loop, then update the file, then access theIEnumerable<T>
again in aforeach
loop to pick up the new data, etc. You only need to callRead
once at the beginning, to get theIEnumerable<T>
.
CsvFileDescription
The Read
and Write
methods need some details about the file they are reading or writing, such as whether the first record contains column names.
As shown in the Reading from a file and Writing to a file examples, you put those details in an object of type CsvFileDescription
, which you then pass to the Read
or Write
method. This prevents lengthy parameter lists, and allows you to use the same details for multiple files.
A CsvFileDescription
object has these properties:
- SeparatorChar
- QuoteAllFields
- FirstLineHasColumnNames
- EnforceCsvColumnAttribute
- FileCultureName
- TextEncoding
- DetectEncodingFromByteOrderMarks
- MaximumNbrExceptions
SeparatorChar
Type:char
Default: ','Applies to: Reading and WritingExample:
CsvFileDescription fd = new CsvFileDescription();fd.SeparatorChar = '\t'; // use tab delimited fileCsvContext cc = new CsvContext();cc.Write(data, "file.csv", fd);
The character used to separate fields in the file. This would be a comma for CSV files, or a '\t' for a tab delimited file.
You can use any character you like, except for white space characters or the double quote (").
QuoteAllFields
Type:bool
Default: false
Applies to:Writing onlyExample:
fd.QuoteAllFields = true; // forces quotes around all fields
When false
, Write
only puts quotes around data fields when needed, to avoid confusion - for example, when the field contains theSeparatorChar
or a line break.
When true
, Write
surrounds all data fields with quotes.
FirstLineHasColumnNames
Type:bool
Default:true
Applies to: Reading and WritingExample:
fd.FirstLineHasColumnNames = false; // first record does not have column headers
When reading a file, tells Read
whether to interpret the data fields in the first record in the file as column headers.
When writing a file, tells Write
whether to write column headers as the first record of the file.
EnforceCsvColumnAttribute
Type:bool
Default: false
Applies to: Reading and WritingExample:
fd.EnforceCsvColumnAttribute = true; // only use fields with [CsvColumn] attribute
When true
, Read
only reads data fields into public fields and properties with the[CsvColumn]
attribute, ignoring all other fields and properties. And,Write
only writes the contents of public fields and properties with the[CsvColumn]
attribute.
When false
, all public fields and properties are used.
FileCultureName
Type:string
Default: current system settingApplies to: Reading and WritingExample:
fd.FileCultureName = "en-US"; // use US style dates and numbers
Different cultures use different ways to write dates and numbers. 23 May 2008 is 5/23/2008 in the United States (en-US) and 23/5/2008 in Germany (de-DE). Use theFileCultureName
field to tellRead
how to interpret the dates and numbers it reads from the file, and to tellWrite
how to write dates and numbers to the file.
By default, the library uses the current language/country setting on your system. So, if your system uses French-Canadian (fr-CA), the library uses that culture unless you override it withFileCultureName
.
The library uses the same culture names as the .NET "CultureInfo
" class (full list of names).
TextEncoding
Type:EncodingDefault:Encoding.UTF8
Applies to: Reading and WritingExample:
fd.TextEncoding = Encoding.Unicode; // use Unicode character encoding
If the files that you read or write are in English, there is no need to set TextEncoding
.
However, if you use languages other than English, the way the characters in your files are encoded may be an issue. You will want to make sure that the encoding used by the library matches the encoding used by any other programs (editors, spreadsheets) that access your files.
Specifically, if you write files with the Euro symbol, you may need to use Unicode encoding, as shown in the example.
DetectEncodingFromByteOrderMarks
Type:bool
Default:true
Applies to: Reading onlyExample:
fd.DetectEncodingFromByteOrderMarks = false; // suppress encoding detection
Related to TextEncoding
. The default normally works fine.
Tells Read
whether to detect the encoding of the input file by looking at the first three bytes of the file. Otherwise, it uses the encoding given in theTextEncoding
property.
MaximumNbrExceptions
Type:int
Default:100Applies to:Reading onlyExample:
fd.MaximumNbrExceptions = -1; // always read entire file before throwing AggregatedException
Sets the maximum number of exceptions that will be aggregated into an AggregatedException
.
To not have any limit and read the entire file no matter how many exceptions you get, setAggregatedException
to -1.
For details about aggregated exceptions, see the error handling section.
CsvColumn Attribute
As shown in the Reading from a file and Writing to a file examples, you can decorate the public fields and properties of your data class with theCsvColumn
attribute to specify such things as the output format for date and number fields.
Use of the CsvColumn
attribute is optional. As long as the EnforceCsvColumnAttribute property of the CsvFileDescription object you pass into Read
or Write
isfalse
, those methods will look at all public fields and properties in the data class. They will then simply use the defaults shown with eachCsvColumn
property below.
The CsvColumn
attribute has these properties:
- Name
- CanBeNull
- NumberStyle
- OutputFormat
- FieldIndex
Name
Type:string
Default:Name of the field or propertyApplies to:Reading and WritingExample:
[CsvColumn(Name = "StartDate")]public DateTime LaunchDate { get; set; }
The Read
and Write
methods normally assume that the data fields in the file have the same names as the corresponding fields or properties in the class. Use theName
property to specify another name for the data field.
CanBeNull
Type:bool
Default: true
Applies to: Reading only[CsvColumn(CanBeNull = false)]public DateTime LaunchDate { get; set; }
If false
, and a record in the input file does not have a value for this field or property, then theRead
method generates aMissingRequiredFieldException exception.
FieldIndex
Type:bool
Default:Int32.MaxValue
Applies to:Reading onlyExample:
[CsvColumn(FieldIndex = 1)]public DateTime LaunchDate { get; set; }
This property is used for both reading and writing, but in slightly different ways.
Reading - The Read
method needs to somehow associate data fields in the input file with field and properties in the data class. If the file has column names in the first record, that's easy -Read
simply matches the column names with the names of the fields and properties in the data class.
However, if the file does not have column names in the first record, Read
needs to look at the order of the data fields in the data records to match them with the fields and properties in the data class. Unfortunately though, the .NET framework does not provide a way to reliably retrieve that order from the class definition. So, you have to specify which field/property comes before which field/property by giving the fields and properties aCsvColumn
attribute with theFieldIndex
property.
The FieldIndex
s do not have to start at 1. They don't have to be consecutive. TheRead
andWrite
methods will simply assume that a field/property comes before some other field/property if itsFieldIndex
is lower.
Writing - The Write
method uses the FieldIndex
of each field or property to figure out in what order to write the data fields to the output file. Field and properties withoutFieldIndex
get written last, in random order.
NumberStyle
Type:NumberStylesDefault:NumberStyles.Any
Applies to: Reading of numeric fields onlyExample:
[CsvColumn(NumberStyle = NumberStyles.HexNumber)]public DateTime LaunchDate { get; set; }
Allows you to determine what number styles are allowed in the input file (list of options).
By default, all styles are permitted, except for one special case. In order to accept hexadecimal numbers that do not start with 0x, useNumberStyles.HexNumber
, as shown in the example.
OutputFormat
Type:string
Default: "G"Applies to: Writing onlyExample:
[CsvColumn(OutputFormat = "dd MMM yy")]public DateTime LaunchDate { get; set; }
Lets you set the output format of numbers and dates/times. The default "G" format works well for both dates and numbers most of the time.
When writing a date/time or number field, the Write
method first determines the type of the field (DateTime
,decimal
,double
, etc.) and then calls theToString
method for that type, with the givenOutputFormat
. So, in the example above, ifLaunchDate
is 23 November 2008, the field written to the file will be "23 Nov 08".
With many formats, the final result depends on the language/country of the file, as set in theFileCultureName property of theCsvFileDescription object. So, if LaunchDate
is 23 November 2008 and you specify the short date format:
[CsvColumn(OutputFormat = "d")]public DateTime LaunchDate { get; set; }
Then, the final value written to the output file will be "11/23/08" if you use US dates (FileCultureName
is set to "en-US"), but "23/11/08" if you use German dates (FileCultureName
is set to "de-DE").
- Format Codes for DateTime
- Standard Numeric Format Strings
- Custom Numeric Format Strings
Error Handling
- Exception
LINQtoCSVException
- BadStreamException
- CsvColumnAttributeRequiredException
- DuplicateFieldIndexException
- RequiredButMissingFieldIndexException
- ToBeWrittenButMissingFieldIndexException
- NameNotInTypeException
- MissingCsvColumnAttributeException
- TooManyDataFieldsException
- TooManyNonCsvColumnDataFieldsException
- MissingFieldIndexException
- MissingRequiredFieldException
- WrongDataFormatException
- AggregatedException
When the Read
and Write
methods detect an error situation, they throw an exception with all information you need to solve the problem. As you would expect, all exceptions are derived from the .NET classException.
Retrieving error information
In addition to such properties as StackTrace
and Message, the Exception
class exposes the Data property. The Read
and Write
methods use that property to provide exception information in a way that is easy for your code to read, while they provide error messages targeted at humans via theMessage
property.
The description for each exception (further below) shows what information is stored in theData
property.
Aggregating exceptions
When the Read
method detects an error while reading data from a file, it does not throw an exception right away, but stores it in a list of typeList<Exception>
. Then, after it has processed the file, it throws a single exception of typeAggregatedException, with the list of exceptions in itsData["InnerExceptionsList"]
property. This allows you to fix all problems with an input file in one go, instead of one by one.
You can limit the number of exceptions that get aggregated this way by setting theMaximumNbrExceptions property of theCsvFileDescription object that you pass to the Read
method. By default,MaximumNbrExceptions
is set to 100. When the limit is reached, theAggregatedException
is thrown right away, with the list of exceptions aggregated so far.
Not all exceptions get aggregated! Before Read
starts reading data from a file, it first processes column names,CsvColumn
attributes, etc. If something goes wrong during that preliminary stage, it throws an exception right away.
Deferred reading
Keep in mind that due to deferred reading, you can get exceptions not only when you invoke the Read
method, but also when you access theIEnumerable<T>
that is returned by theRead
method.
Example
The following code reads a file and processes exceptions. To show how to use theData
property, it includes some special processing for theDuplicateFieldIndexException
- thrown when theRead
andWrite
methods detect two fields or properties with the sameFieldIndex.
public static void ShowErrorMessage(string errorMessage){ // show errorMessage to user // .....}public static void ReadFileWithExceptionHandling(){ try { CsvContext cc = new CsvContext(); CsvFileDescription inputFileDescription = new CsvFileDescription { MaximumNbrExceptions = 50 // limit number of aggregated exceptions to 50 }; IEnumerable<Product> products = cc.Read<Product>("products.csv", inputFileDescription); // Do data processing // ........... } catch(AggregatedException ae) { // Process all exceptions generated while processing the file List<Exception> innerExceptionsList = (List<Exception>)ae.Data["InnerExceptionsList"]; foreach (Exception e in innerExceptionsList) { ShowErrorMessage(e.Message); } } catch(DuplicateFieldIndexException dfie) { // name of the class used with the Read method - in this case "Product" string typeName = Convert.ToString(dfie.Data["TypeName"]); // Names of the two fields or properties that have the same FieldIndex string fieldName = Convert.ToString(dfie.Data["FieldName"]); string fieldName2 = Convert.ToString(dfie.Data["FieldName2"]); // Actual FieldIndex that the two fields have in common int commonFieldIndex = Convert.ToInt32(dfie.Data["Index"]); // Do some processing with this information // ......... // Inform user of error situation ShowErrorMessage(dfie.Message); } catch(Exception e) { ShowErrorMessage(e.Message); }}
BadStreamException
This exception exposes the same properties as Exception.
Thrown when a stream is passed to Read
, which is either null
, or does not support Seek
. The stream has to support Seek
, otherwise it cannot be rewound when the IEnumarable
returned byRead
is accessed.
CsvColumnAttributeRequiredException
This exception exposes the same properties as Exception.
Thrown when the CsvFileDescription
object that has been passed toRead
has bothFirstLineHasColumnNames and EnforceCsvColumnAttribute set to false
.
If there are no column names in the file, then Read
relies on theFieldIndex
of each field or property in the data class to match them with the data fields in the file. However, ifEnforceCsvColumnAttribute
isfalse
, that implies that fields or properties without theCsvColumn
attribute can also be used to accept data, while they do not have aFieldIndex
.
DuplicateFieldIndexException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the class with the offending fields/propertiesData["FieldName"]
string
Fields or properties with a duplicate FieldIndex
Data["FieldName2"]
Data["Index"]
int
Common FieldIndex
Thrown when two or more fields or properties have the same FieldIndex.
RequiredButMissingFieldIndexException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the class with the offending field/propertyData["FieldName"]
string
Field or property without FieldIndex
When there are no column names in the first record in the file (FirstLineHasColumnNames isfalse
), each required field (CanBeNull attribute set tofalse
) must have aFieldIndex attribute, otherwise it cannot be read from the file.
ToBeWrittenButMissingFieldIndexException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the class with the offending field/propertyData["FieldName"]
string
Field or property without FieldIndex
When writing a file without column names in the first record, you will want to make sure that the data fields appear in each line in a well defined order. If that order were random, it would be impossible for some other program to reliably process the file.
So, when the Write
method is given a CsvFileDescription with FirstLineHasColumnNames as false
, and it finds a field or property that doesn't have aFieldIndex
, it throws aToBeWrittenButMissingFieldIndexException
.
NameNotInTypeException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the class missing the field/propertyData["FieldName"]
string
Field or property that isn't foundData["FileName"]
string
Name of the input fileIf the Read
method is given a CsvFileDescription with FirstLineHasColumnNames as true
, and one of the column names in the first record in the file does not match a field or property, it throws aNameNotInTypeException
.
MissingCsvColumnAttributeException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the class with the offending field/propertyData["FieldName"]
string
Field or property without CsvColumn
attributeData["FileName"]
string
Name of the input fileThe Read
method may throw this exception when it is given a CsvFileDescription with both FirstLineHasColumnNames and EnforceCsvColumnAttribute as true
. When Read
reads the column names from the first record, one of those column names may match a field or property that doesn't have aCsvColumn
attribute, even though only fields and properties with a CsvColumn
attribute can be used. When that happens,Read
throws aMissingCsvColumnAttributeException
.
TooManyDataFieldsException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the data classData["LineNbr"]
int
Line in the input file with an excess data fieldData["FileName"]
string
Name of the input fileThrown when a record in the input file has more data fields than there are public fields and properties in the data class.
TooManyNonCsvColumnDataFieldsException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the data classData["LineNbr"]
int
Line in the input file with an excess data fieldData["FileName"]
string
Name of the input fileWhen only fields or properties that have a CsvColumn
attribute are used (Read
is given aCsvFileDescription with EnforceCsvColumnAttribute as true
), and a record in the input file has more data fields than there are fields and properties with theCsvColumn
attribute, aTooManyNonCsvColumnDataFieldsException
is thrown.
MissingFieldIndexException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the data classData["LineNbr"]
int
Line with offending fieldData["FileName"]
string
Name of the input fileIf there are no column names in the first record of the input file (Read
is given aCsvFileDescription withFirstLineHasColumnNames as false
), then Read
relies on theFieldIndex of the fields and properties in the data class to match them with the data fields in the file.
When a record in the input file has more data fields than there are fields and properties in the data class with aFieldIndex
, then aMissingFieldIndexException
is thrown.
MissingRequiredFieldException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the class with the required field/propertyData["FieldName"]
string
Name of the required field/propertyData["LineNbr"]
int
Line where missing field should have beenData["FileName"]
string
Name of the input fileThrown when a record from the input file does not have a value for a required field or property (CanBeNull property of theCsvColumn attribute set to false
).
Difference between null and empty string
Empty strings and strings consisting of only white space need to be surrounded by quotes, so they are recognized as something other thannull
.
These input lines both have the data fields "abc", null, and "def":
abc,,defabc, ,def
While this line has the data fields "abc", followed by the empty string, followed by "def":
abc,"",def
and this line has the data fields "abc", followed by a string with three spaces, followed by "def":
abc," ",def
WrongDataFormatException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the class with the field/propertyData["FieldName"]
string
Name of the field/propertyData["FieldValue"]
string
The offending data valueData["LineNbr"]
int
Line with offending data valueData["FileName"]
string
Name of the input fileThrown when a field has the wrong format. For example, a numeric field with the value "abc".
AggregatedException
Additional Properties - This exception exposes the same properties asException, plus these additional properties:
PropertyTypeDescriptionData["TypeName"]
string
Name of the data class used by Read
Data["FileName"]
string
Name of the input fileData["InnerExceptionsList"]
List<Exception>
List of Exception
sUsed to aggregate exceptions generated while reading a file (more details).
转自:http://www.aspnetperformance.com/post/LINQ-to-CSV-library.aspx
- LINQ to CSV library
- linq,linq to sql
- DataTable to CSV&& CSV to DataTable
- linq学习 Linq to DataSet
- Linq初探--Linq to SQL
- LINQ(LINQ to Entities)
- LINQ解析:LINQ to Objects
- LINQ(LINQ to Entities)
- LINQ(LINQ to Entities)
- LINQ(LINQ to DataSet)
- csv to sql server
- csv to mysql
- .csv to .xls
- CSV to Entity
- table to csv
- Convert datatab to csv
- weka CSV to ARff
- pyspark-csv To DataFrame
- JAVA单例模式的优缺点
- opencv调用摄像头录制视频
- Oracle DB 存储增强
- bash 编程应用及调试
- Wdindows7 32bit 下 Eclispe 创建第一个Cocos2dx Hello World!的一些个人错误总结
- LINQ to CSV library
- C++隐藏与覆盖
- 整数划分算法原理与实现
- linux下 ps 配合 kill 命令的用法
- tdifw tdi_event_receive_datagram 问题
- Java中数组的特性
- QT技术应用_2D绘图(转)
- 手机市场已经人满为患,别往里挤了!
- coco2d-x 2.1.4 win7配置