LINQ to CSV library

来源:互联网 发布:js 对象类型数组 编辑:程序博客网 时间:2024/05/29 10:55

by Matt Perdeck12. 九月 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.

  1. Download the zip file with the source code, and unzip in a directory.
  2. Open the Code\LINQtoCSV.sln file in Visual Studio.
  3. You'll find that the sources are organized in a solution, with these elements:
    1. Project LINQtoCSV is the actual library.
    2. Project SampleCode has the sample code shown in this article.
    3. Project TestConsoleApplication is a working console application that exercises most of the features of the library. The code is heavily documented.
    4. 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.
  4. 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

    1. In your project, add a reference to the LINQtoCSV.dll you generated duringInstallation.
    2. The file will be read into an IEnumerable<T>, where T 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.

    1. Import the LINQtoCSV namespace at the top of the source file where you'll be reading the file:
using LINQtoCSV;

    1. 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).

    1. 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.

    1. Read the file into an IEnumerable<T> using the CsvContext 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>.

    1. You can now access products via a LINQ query, a foreach 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.

    1. In your project, add a reference to LINQtoCSV.dll.
    2. The Write method takes a IEnumerable<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.

    1. Import the LINQtoCSV namespace at the top of the source file where you'll be writing the file:
using LINQtoCSV;
    1. 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// ...
    1. 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};

    1. Create a CsvContext object:
CsvContext cc = new CsvContext();
    1. Invoke the Write method exposed by the CsvContext 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 theCsvFileDescription 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 theCsvFileDescription 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 an IEnumerable<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 aforeach), 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 theIEnumerable<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:charDefault: ','Applies to: Reading and Writing

Example:

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:boolDefault: falseApplies to:Writing only

Example:

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:boolDefault:trueApplies to: Reading and Writing

Example:

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:boolDefault: falseApplies to: Reading and Writing

Example:

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:stringDefault: current system settingApplies to: Reading and Writing

Example:

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.UTF8Applies to: Reading and Writing

Example:

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:boolDefault:trueApplies to: Reading only

Example:

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:intDefault:100Applies to:Reading only

Example:

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:stringDefault:Name of the field or propertyApplies to:Reading and Writing

Example:

[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:boolDefault: trueApplies 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:boolDefault:Int32.MaxValueApplies to:Reading only

Example:

[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 FieldIndexs 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.AnyApplies to: Reading of numeric fields only

Example:

[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:stringDefault: "G"Applies to: Writing only

Example:

[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"]stringName of the class with the offending fields/propertiesData["FieldName"]stringFields or properties with a duplicate FieldIndexData["FieldName2"]Data["Index"]intCommon 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"]stringName of the class with the offending field/propertyData["FieldName"]stringField 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"]stringName of the class with the offending field/propertyData["FieldName"]stringField 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"]stringName of the class missing the field/propertyData["FieldName"]stringField or property that isn't foundData["FileName"]stringName of the input file

If 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"]stringName of the class with the offending field/propertyData["FieldName"]stringField or property without CsvColumn attributeData["FileName"]stringName of the input file

The 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"]stringName of the data classData["LineNbr"]intLine in the input file with an excess data fieldData["FileName"]stringName of the input file

Thrown 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"]stringName of the data classData["LineNbr"]intLine in the input file with an excess data fieldData["FileName"]stringName of the input file

When 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"]stringName of the data classData["LineNbr"]intLine with offending fieldData["FileName"]stringName of the input file

If 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"]stringName of the class with the required field/propertyData["FieldName"]stringName of the required field/propertyData["LineNbr"]intLine where missing field should have beenData["FileName"]stringName of the input file

Thrown 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"]stringName of the class with the field/propertyData["FieldName"]stringName of the field/propertyData["FieldValue"]stringThe offending data valueData["LineNbr"]intLine with offending data valueData["FileName"]stringName of the input file

Thrown 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"]stringName of the data class used by ReadData["FileName"]stringName of the input fileData["InnerExceptionsList"]List<Exception>List of Exceptions

Used to aggregate exceptions generated while reading a file (more details).

 

转自:http://www.aspnetperformance.com/post/LINQ-to-CSV-library.aspx

 

原创粉丝点击