Reading CSV files using C#










143














I'm writing a simple import application and need to read a CSV file, show result in a DataGrid and show corrupted lines of the CSV file in another grid. For example, show the lines that are shorter than 5 values in another grid. I'm trying to do that like this:



StreamReader sr = new StreamReader(FilePath);
importingData = new Account();
string line;
string row = new string [5];
while ((line = sr.ReadLine()) != null)

row = line.Split(',');

importingData.Add(new Transaction

Date = DateTime.Parse(row[0]),
Reference = row[1],
Description = row[2],
Amount = decimal.Parse(row[3]),
Category = (Category)Enum.Parse(typeof(Category), row[4])
);



but it's very difficult to operate on arrays in this case. Is there any better way to split the values?










share|improve this question























  • Thanks for your solution. Consider posting it as an answer post - including it in the question doesn't help its readability.
    – BartoszKP
    Jun 22 '15 at 12:59










  • see also stackoverflow.com/questions/1941392/…
    – Malcolm
    Oct 28 '17 at 16:09















143














I'm writing a simple import application and need to read a CSV file, show result in a DataGrid and show corrupted lines of the CSV file in another grid. For example, show the lines that are shorter than 5 values in another grid. I'm trying to do that like this:



StreamReader sr = new StreamReader(FilePath);
importingData = new Account();
string line;
string row = new string [5];
while ((line = sr.ReadLine()) != null)

row = line.Split(',');

importingData.Add(new Transaction

Date = DateTime.Parse(row[0]),
Reference = row[1],
Description = row[2],
Amount = decimal.Parse(row[3]),
Category = (Category)Enum.Parse(typeof(Category), row[4])
);



but it's very difficult to operate on arrays in this case. Is there any better way to split the values?










share|improve this question























  • Thanks for your solution. Consider posting it as an answer post - including it in the question doesn't help its readability.
    – BartoszKP
    Jun 22 '15 at 12:59










  • see also stackoverflow.com/questions/1941392/…
    – Malcolm
    Oct 28 '17 at 16:09













143












143








143


66





I'm writing a simple import application and need to read a CSV file, show result in a DataGrid and show corrupted lines of the CSV file in another grid. For example, show the lines that are shorter than 5 values in another grid. I'm trying to do that like this:



StreamReader sr = new StreamReader(FilePath);
importingData = new Account();
string line;
string row = new string [5];
while ((line = sr.ReadLine()) != null)

row = line.Split(',');

importingData.Add(new Transaction

Date = DateTime.Parse(row[0]),
Reference = row[1],
Description = row[2],
Amount = decimal.Parse(row[3]),
Category = (Category)Enum.Parse(typeof(Category), row[4])
);



but it's very difficult to operate on arrays in this case. Is there any better way to split the values?










share|improve this question















I'm writing a simple import application and need to read a CSV file, show result in a DataGrid and show corrupted lines of the CSV file in another grid. For example, show the lines that are shorter than 5 values in another grid. I'm trying to do that like this:



StreamReader sr = new StreamReader(FilePath);
importingData = new Account();
string line;
string row = new string [5];
while ((line = sr.ReadLine()) != null)

row = line.Split(',');

importingData.Add(new Transaction

Date = DateTime.Parse(row[0]),
Reference = row[1],
Description = row[2],
Amount = decimal.Parse(row[3]),
Category = (Category)Enum.Parse(typeof(Category), row[4])
);



but it's very difficult to operate on arrays in this case. Is there any better way to split the values?







c# csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 25 '16 at 0:42









user1623521

294113




294113










asked Aug 17 '10 at 22:30









ilkinilkin

1,16231019




1,16231019











  • Thanks for your solution. Consider posting it as an answer post - including it in the question doesn't help its readability.
    – BartoszKP
    Jun 22 '15 at 12:59










  • see also stackoverflow.com/questions/1941392/…
    – Malcolm
    Oct 28 '17 at 16:09
















  • Thanks for your solution. Consider posting it as an answer post - including it in the question doesn't help its readability.
    – BartoszKP
    Jun 22 '15 at 12:59










  • see also stackoverflow.com/questions/1941392/…
    – Malcolm
    Oct 28 '17 at 16:09















Thanks for your solution. Consider posting it as an answer post - including it in the question doesn't help its readability.
– BartoszKP
Jun 22 '15 at 12:59




Thanks for your solution. Consider posting it as an answer post - including it in the question doesn't help its readability.
– BartoszKP
Jun 22 '15 at 12:59












see also stackoverflow.com/questions/1941392/…
– Malcolm
Oct 28 '17 at 16:09




see also stackoverflow.com/questions/1941392/…
– Malcolm
Oct 28 '17 at 16:09












10 Answers
10






active

oldest

votes


















323














Don't reinvent the wheel. Take advantage of what's already in .NET BCL.



  • add a reference to the Microsoft.VisualBasic (yes, it says VisualBasic but it works in C# just as well - remember that at the end it is all just IL)

  • use the Microsoft.VisualBasic.FileIO.TextFieldParser class to parse CSV file

Here is the sample code:



using (TextFieldParser parser = new TextFieldParser(@"c:temptest.csv"))

parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)

//Processing row
string fields = parser.ReadFields();
foreach (string field in fields)

//TODO: Process field





It works great for me in my C# projects.



Here are some more links/informations:



  • MSDN: Read From Comma-Delimited Text Files in Visual Basic

  • MSDN: TextFieldParser Class





share|improve this answer


















  • 13




    I REALLY wish there was a way that didn't use VB libraries, but this worked perfectly! Thank you!
    – rotard
    Feb 3 '12 at 14:05






  • 3




    +1: I just broke the lumenworks Fast CSV reader on a 53Mb file. Looks like the line caching failed after 43,000 rows and scrambled the buffer. Tried the VB TextFieldParser and it did the trick. Thanks
    – Gone Coding
    May 21 '12 at 12:54






  • 9




    +1 Great answer, as I find many people don't know this class exists. One thing for future viewers to note is that setting parser.TextFieldType = FieldType.Delimited; is not necessary if you call parser.SetDelimiters(",");, as the method sets the TextFieldType property for you.
    – Brian
    Nov 5 '13 at 22:25







  • 8




    Also check this out: dotnetperls.com/textfieldparser. TextFieldParser has worse performance than String.Split and StreamReader. However, there is a big difference between string.Split and TextFieldParser. TextFieldParser handles strange cases like having a comma in a column: you can name a column like "text with quote"", and comma", and you can get the correct value text with quote", and comma instead of wrongly separated values. So you may want to opt for String.Split if you csv is very simple.
    – Yongwei Wu
    Dec 10 '13 at 3:53







  • 4




    Note that you may need to add a reference to Microsoft.VisualBasic to use this. Right-click on your project in Visual Studio, then choose Add > Reference, and check the box for Microsoft.VisualBasic.
    – Derek Kurth
    Jan 2 '15 at 16:00


















30














My experience is that there are many different csv formats. Specially how they handle escaping of quotes and delimiters within a field.



These are the variants I have ran into:



  • quotes are quoted and doubled (excel) i.e. 15" -> field1,"15""",field3

  • quotes are not changed unless the field is quoted for some other reason. i.e. 15" -> field1,15",fields3

  • quotes are escaped with . i.e. 15" -> field1,"15"",field3

  • quotes are not changed at all (this is not always possible to parse correctly)

  • delimiter is quoted (excel). i.e. a,b -> field1,"a,b",field3

  • delimiter is escaped with . i.e. a,b -> field1,a,b,field3

I have tried many of the existing csv parsers but there is not a single one that can handle the variants I have ran into. It is also difficult to find out from the documentation which escaping variants the parsers support.



In my projects I now use either the VB TextFieldParser or a custom splitter.






share|improve this answer
















  • 1




    Love this answer for the test cases you provided!
    – Matthew Rodatus
    Oct 4 '13 at 14:32






  • 2




    The main problem is that most implementations don't care about RFC 4180 that describes the CSV format and how delimiters should be escaped.
    – Jenny O'Reilly
    May 29 '15 at 7:00


















19














I recommend CsvHelper from Nuget.



(Adding a reference to Microsoft.VisualBasic just doesn't feel right, it's not only ugly, it's probably not even cross-platform.)






share|improve this answer






















  • It's exactly as cross-platform as C# is.
    – PRMan
    Sep 6 '17 at 23:29










  • wrong, Microsoft.VisualBasic.dll in Linux comes from Mono sources, which has a different implementation than Microsoft's and there are some things that are not implemented, for example: stackoverflow.com/questions/6644165/…
    – knocte
    Sep 7 '17 at 7:59










  • (Plus, VB language has never had any focus under the companies that have been involved in creating/developing the Mono project, so it's way behind in terms of efforts, compared to the C# ecosystem/tooling.)
    – knocte
    Sep 7 '17 at 8:00






  • 1




    Having played with both, I'd add that CsvHelper comes with a built-in row to class mapper; it allows for variations in the column headers (if present), and even apparently variations in the column order (though I've not tested the latter myself). All in all it feels much more "high-level" than TextFieldParser.
    – David
    Jul 9 '18 at 15:57


















10














Sometimes using libraries are cool when you do not want to reinvent the wheel, but in this case one can do the same job with fewer lines of code and easier to read compared to using libraries.
Here is a different approach which I find very easy to use.



  1. In this example, I use StreamReader to read the file

  2. Regex to detect the delimiter from each line(s).

  3. An array to collect the columns from index 0 to n


using (StreamReader reader = new StreamReader(fileName))

string line;

while ((line = reader.ReadLine()) != null)

//Define pattern
Regex CSVParser = new Regex(",(?=(?:[^"]*"[^"]*")*(?![^"]*"))");

//Separating columns to array
string X = CSVParser.Split(line);

/* Do something with X */







share|improve this answer


















  • 4




    Surely that has problems with data that itself contains new lines?
    – Doogal
    Apr 7 '16 at 14:41










  • Now CSV datafiles are not know for containing empty lines between data, but if you have a source that does that, in that case i would just simple done a simple regex test to remove whitespaces or lines containing nothing before running the reader. check here for different examples: stackoverflow.com/questions/7647716/…
    – Mana
    Apr 11 '16 at 8:38






  • 1




    Surely a char-based approach is more natural for this sort of problem than a regex. Depending on the presence of quotation marks the behavior is supposed to be different.
    – Casey
    May 9 '16 at 1:37


















5














CSV can get complicated real fast.


Use something robust and well-tested:

FileHelpers:
www.filehelpers.net





The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.





share|improve this answer


















  • 5




    I think FileHelper is trying to do to much in one go. Parsing files is a 2 step process where you first split lines into fields and then parse the fields into data. Combining the functions makes it difficult to handle things like master-detail and line filtering.
    – adrianm
    Aug 18 '10 at 10:11


















3














I use this here:



http://www.codeproject.com/KB/database/GenericParser.aspx



Last time I was looking for something like this I found it as an answer to this question.






share|improve this answer






























    2














    Another one to this list, Cinchoo ETL - an open source library to read and write CSV files



    For a sample CSV file below



    Id, Name
    1, Tom
    2, Mark


    Quickly you can load them using library as below



    using (var reader = new ChoCSVReader("test.csv").WithFirstLineHeader())

    foreach (dynamic item in reader)

    Console.WriteLine(item.Id);
    Console.WriteLine(item.Name);




    If you have POCO class matching the CSV file



    public class Employee

    public int Id get; set;
    public string Name get; set;



    You can use it to load the CSV file as below



    using (var reader = new ChoCSVReader<Employee>("test.csv").WithFirstLineHeader())

    foreach (var item in reader)

    Console.WriteLine(item.Id);
    Console.WriteLine(item.Name);




    Please check out articles at CodeProject on how to use it.



    Disclaimer: I'm the author of this library






    share|improve this answer






















    • Hi, can you load csv to Sql table - I dont know the header in the CSV table before hand. Just mirror whats in csv to Sql table
      – aggie
      Nov 15 '18 at 4:35










    • Yes, you can. please see this link stackoverflow.com/questions/20759302/…
      – RajN
      Nov 15 '18 at 13:14


















    1














    private static DataTable ConvertCSVtoDataTable(string strFilePath)

    DataTable dt = new DataTable();
    using (StreamReader sr = new StreamReader(strFilePath))

    string headers = sr.ReadLine().Split(',');
    foreach (string header in headers)

    dt.Columns.Add(header);

    while (!sr.EndOfStream)

    string rows = sr.ReadLine().Split(',');
    DataRow dr = dt.NewRow();
    for (int i = 0; i < headers.Length; i++)

    dr[i] = rows[i];

    dt.Rows.Add(dr);




    return dt;


    private static void WriteToDb(DataTable dt)

    string connectionString =
    "Data Source=localhost;" +
    "Initial Catalog=Northwind;" +
    "Integrated Security=SSPI;";

    using (SqlConnection con = new SqlConnection(connectionString))

    using (SqlCommand cmd = new SqlCommand("spInsertTest", con))

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add("@policyID", SqlDbType.Int).Value = 12;
    cmd.Parameters.Add("@statecode", SqlDbType.VarChar).Value = "blagh2";
    cmd.Parameters.Add("@county", SqlDbType.VarChar).Value = "blagh3";

    con.Open();
    cmd.ExecuteNonQuery();









    share|improve this answer




























      0














      First of all need to understand what is CSV and how to write it.



      1. Every next string ( /r/n ) is next "table" row.

      2. "Table" cells is separated by some delimiter symbol. Most often used symbols is t or ,

      3. Every cell possibly can contain this delimiter symbol (cell must to start with quotes symbol and ends with this symbol in this case)

      4. Every cell possibly can contains /r/n sybols (cell must to start with quotes symbol and ends with this symbol in this case)

      The easiest way for C#/Visual Basic to work with CSV files is to use standard Microsoft.VisualBasic library. You just need to add needed reference, and the following string to your class:



      using Microsoft.VisualBasic.FileIO;


      Yes, you can use it in C#, don't worry. This library can read relatively big files and supports all of needed rules, so you will be able to work with all of CSV files.



      Some time ago I had wrote simple class for CSV read/write based on this library. Using this simple class you will be able to work with CSV like with 2 dimensions array.
      You can find my class by the following link:
      https://github.com/ukushu/DataExporter



      Simple example of using:



      Csv csv = new Csv("t");//delimiter symbol

      csv.FileOpen("c:\file1.csv");

      var row1Cell6Value = csv.Rows[0][5];

      csv.AddRow("asdf","asdffffff","5")

      csv.FileSave("c:\file2.csv");





      share|improve this answer






























        0














        To complete the previous answers, one may need a collection of objects from his CSV File, either parsed by the TextFieldParser or the string.Split method, and then each line converted to an object via Reflection. You obviously first need to define a class that matches the lines of the CSV file.



        I used the simple CSV Serializer from Michael Kropat found here: Generic class to CSV (all properties)
        and reused his methods to get the fields and properties of the wished class.



        I deserialize my CSV file with the following method:



        public static IEnumerable<T> ReadCsvFileTextFieldParser<T>(string fileFullPath, string delimiter = ";") where T : new()

        if (!File.Exists(fileFullPath))

        return null;


        var list = new List<T>();
        var csvFields = GetAllFieldOfClass<T>();
        var fieldDict = new Dictionary<int, MemberInfo>();

        using (TextFieldParser parser = new TextFieldParser(fileFullPath))

        parser.SetDelimiters(delimiter);

        bool headerParsed = false;

        while (!parser.EndOfData)

        //Processing row
        string rowFields = parser.ReadFields();
        if (!headerParsed)

        for (int i = 0; i < rowFields.Length; i++)

        // First row shall be the header!
        var csvField = csvFields.Where(f => f.Name == rowFields[i]).FirstOrDefault();
        if (csvField != null)

        fieldDict.Add(i, csvField);


        headerParsed = true;

        else

        T newObj = new T();
        for (int i = 0; i < rowFields.Length; i++)

        var csvFied = fieldDict[i];
        var record = rowFields[i];

        if (csvFied is FieldInfo)

        ((FieldInfo)csvFied).SetValue(newObj, record);

        else if (csvFied is PropertyInfo)

        var pi = (PropertyInfo)csvFied;
        pi.SetValue(newObj, Convert.ChangeType(record, pi.PropertyType), null);

        else

        throw new Exception("Unhandled case.");


        if (newObj != null)

        list.Add(newObj);




        return list;


        public static IEnumerable<MemberInfo> GetAllFieldOfClass<T>()

        return
        from mi in typeof(T).GetMembers(BindingFlags.Public





        share|improve this answer






















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3507498%2freading-csv-files-using-c-sharp%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          10 Answers
          10






          active

          oldest

          votes








          10 Answers
          10






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          323














          Don't reinvent the wheel. Take advantage of what's already in .NET BCL.



          • add a reference to the Microsoft.VisualBasic (yes, it says VisualBasic but it works in C# just as well - remember that at the end it is all just IL)

          • use the Microsoft.VisualBasic.FileIO.TextFieldParser class to parse CSV file

          Here is the sample code:



          using (TextFieldParser parser = new TextFieldParser(@"c:temptest.csv"))

          parser.TextFieldType = FieldType.Delimited;
          parser.SetDelimiters(",");
          while (!parser.EndOfData)

          //Processing row
          string fields = parser.ReadFields();
          foreach (string field in fields)

          //TODO: Process field





          It works great for me in my C# projects.



          Here are some more links/informations:



          • MSDN: Read From Comma-Delimited Text Files in Visual Basic

          • MSDN: TextFieldParser Class





          share|improve this answer


















          • 13




            I REALLY wish there was a way that didn't use VB libraries, but this worked perfectly! Thank you!
            – rotard
            Feb 3 '12 at 14:05






          • 3




            +1: I just broke the lumenworks Fast CSV reader on a 53Mb file. Looks like the line caching failed after 43,000 rows and scrambled the buffer. Tried the VB TextFieldParser and it did the trick. Thanks
            – Gone Coding
            May 21 '12 at 12:54






          • 9




            +1 Great answer, as I find many people don't know this class exists. One thing for future viewers to note is that setting parser.TextFieldType = FieldType.Delimited; is not necessary if you call parser.SetDelimiters(",");, as the method sets the TextFieldType property for you.
            – Brian
            Nov 5 '13 at 22:25







          • 8




            Also check this out: dotnetperls.com/textfieldparser. TextFieldParser has worse performance than String.Split and StreamReader. However, there is a big difference between string.Split and TextFieldParser. TextFieldParser handles strange cases like having a comma in a column: you can name a column like "text with quote"", and comma", and you can get the correct value text with quote", and comma instead of wrongly separated values. So you may want to opt for String.Split if you csv is very simple.
            – Yongwei Wu
            Dec 10 '13 at 3:53







          • 4




            Note that you may need to add a reference to Microsoft.VisualBasic to use this. Right-click on your project in Visual Studio, then choose Add > Reference, and check the box for Microsoft.VisualBasic.
            – Derek Kurth
            Jan 2 '15 at 16:00















          323














          Don't reinvent the wheel. Take advantage of what's already in .NET BCL.



          • add a reference to the Microsoft.VisualBasic (yes, it says VisualBasic but it works in C# just as well - remember that at the end it is all just IL)

          • use the Microsoft.VisualBasic.FileIO.TextFieldParser class to parse CSV file

          Here is the sample code:



          using (TextFieldParser parser = new TextFieldParser(@"c:temptest.csv"))

          parser.TextFieldType = FieldType.Delimited;
          parser.SetDelimiters(",");
          while (!parser.EndOfData)

          //Processing row
          string fields = parser.ReadFields();
          foreach (string field in fields)

          //TODO: Process field





          It works great for me in my C# projects.



          Here are some more links/informations:



          • MSDN: Read From Comma-Delimited Text Files in Visual Basic

          • MSDN: TextFieldParser Class





          share|improve this answer


















          • 13




            I REALLY wish there was a way that didn't use VB libraries, but this worked perfectly! Thank you!
            – rotard
            Feb 3 '12 at 14:05






          • 3




            +1: I just broke the lumenworks Fast CSV reader on a 53Mb file. Looks like the line caching failed after 43,000 rows and scrambled the buffer. Tried the VB TextFieldParser and it did the trick. Thanks
            – Gone Coding
            May 21 '12 at 12:54






          • 9




            +1 Great answer, as I find many people don't know this class exists. One thing for future viewers to note is that setting parser.TextFieldType = FieldType.Delimited; is not necessary if you call parser.SetDelimiters(",");, as the method sets the TextFieldType property for you.
            – Brian
            Nov 5 '13 at 22:25







          • 8




            Also check this out: dotnetperls.com/textfieldparser. TextFieldParser has worse performance than String.Split and StreamReader. However, there is a big difference between string.Split and TextFieldParser. TextFieldParser handles strange cases like having a comma in a column: you can name a column like "text with quote"", and comma", and you can get the correct value text with quote", and comma instead of wrongly separated values. So you may want to opt for String.Split if you csv is very simple.
            – Yongwei Wu
            Dec 10 '13 at 3:53







          • 4




            Note that you may need to add a reference to Microsoft.VisualBasic to use this. Right-click on your project in Visual Studio, then choose Add > Reference, and check the box for Microsoft.VisualBasic.
            – Derek Kurth
            Jan 2 '15 at 16:00













          323












          323








          323






          Don't reinvent the wheel. Take advantage of what's already in .NET BCL.



          • add a reference to the Microsoft.VisualBasic (yes, it says VisualBasic but it works in C# just as well - remember that at the end it is all just IL)

          • use the Microsoft.VisualBasic.FileIO.TextFieldParser class to parse CSV file

          Here is the sample code:



          using (TextFieldParser parser = new TextFieldParser(@"c:temptest.csv"))

          parser.TextFieldType = FieldType.Delimited;
          parser.SetDelimiters(",");
          while (!parser.EndOfData)

          //Processing row
          string fields = parser.ReadFields();
          foreach (string field in fields)

          //TODO: Process field





          It works great for me in my C# projects.



          Here are some more links/informations:



          • MSDN: Read From Comma-Delimited Text Files in Visual Basic

          • MSDN: TextFieldParser Class





          share|improve this answer














          Don't reinvent the wheel. Take advantage of what's already in .NET BCL.



          • add a reference to the Microsoft.VisualBasic (yes, it says VisualBasic but it works in C# just as well - remember that at the end it is all just IL)

          • use the Microsoft.VisualBasic.FileIO.TextFieldParser class to parse CSV file

          Here is the sample code:



          using (TextFieldParser parser = new TextFieldParser(@"c:temptest.csv"))

          parser.TextFieldType = FieldType.Delimited;
          parser.SetDelimiters(",");
          while (!parser.EndOfData)

          //Processing row
          string fields = parser.ReadFields();
          foreach (string field in fields)

          //TODO: Process field





          It works great for me in my C# projects.



          Here are some more links/informations:



          • MSDN: Read From Comma-Delimited Text Files in Visual Basic

          • MSDN: TextFieldParser Class






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 22 '15 at 13:01









          BartoszKP

          26.7k1065103




          26.7k1065103










          answered Aug 18 '10 at 2:42









          David PokludaDavid Pokluda

          8,52252226




          8,52252226







          • 13




            I REALLY wish there was a way that didn't use VB libraries, but this worked perfectly! Thank you!
            – rotard
            Feb 3 '12 at 14:05






          • 3




            +1: I just broke the lumenworks Fast CSV reader on a 53Mb file. Looks like the line caching failed after 43,000 rows and scrambled the buffer. Tried the VB TextFieldParser and it did the trick. Thanks
            – Gone Coding
            May 21 '12 at 12:54






          • 9




            +1 Great answer, as I find many people don't know this class exists. One thing for future viewers to note is that setting parser.TextFieldType = FieldType.Delimited; is not necessary if you call parser.SetDelimiters(",");, as the method sets the TextFieldType property for you.
            – Brian
            Nov 5 '13 at 22:25







          • 8




            Also check this out: dotnetperls.com/textfieldparser. TextFieldParser has worse performance than String.Split and StreamReader. However, there is a big difference between string.Split and TextFieldParser. TextFieldParser handles strange cases like having a comma in a column: you can name a column like "text with quote"", and comma", and you can get the correct value text with quote", and comma instead of wrongly separated values. So you may want to opt for String.Split if you csv is very simple.
            – Yongwei Wu
            Dec 10 '13 at 3:53







          • 4




            Note that you may need to add a reference to Microsoft.VisualBasic to use this. Right-click on your project in Visual Studio, then choose Add > Reference, and check the box for Microsoft.VisualBasic.
            – Derek Kurth
            Jan 2 '15 at 16:00












          • 13




            I REALLY wish there was a way that didn't use VB libraries, but this worked perfectly! Thank you!
            – rotard
            Feb 3 '12 at 14:05






          • 3




            +1: I just broke the lumenworks Fast CSV reader on a 53Mb file. Looks like the line caching failed after 43,000 rows and scrambled the buffer. Tried the VB TextFieldParser and it did the trick. Thanks
            – Gone Coding
            May 21 '12 at 12:54






          • 9




            +1 Great answer, as I find many people don't know this class exists. One thing for future viewers to note is that setting parser.TextFieldType = FieldType.Delimited; is not necessary if you call parser.SetDelimiters(",");, as the method sets the TextFieldType property for you.
            – Brian
            Nov 5 '13 at 22:25







          • 8




            Also check this out: dotnetperls.com/textfieldparser. TextFieldParser has worse performance than String.Split and StreamReader. However, there is a big difference between string.Split and TextFieldParser. TextFieldParser handles strange cases like having a comma in a column: you can name a column like "text with quote"", and comma", and you can get the correct value text with quote", and comma instead of wrongly separated values. So you may want to opt for String.Split if you csv is very simple.
            – Yongwei Wu
            Dec 10 '13 at 3:53







          • 4




            Note that you may need to add a reference to Microsoft.VisualBasic to use this. Right-click on your project in Visual Studio, then choose Add > Reference, and check the box for Microsoft.VisualBasic.
            – Derek Kurth
            Jan 2 '15 at 16:00







          13




          13




          I REALLY wish there was a way that didn't use VB libraries, but this worked perfectly! Thank you!
          – rotard
          Feb 3 '12 at 14:05




          I REALLY wish there was a way that didn't use VB libraries, but this worked perfectly! Thank you!
          – rotard
          Feb 3 '12 at 14:05




          3




          3




          +1: I just broke the lumenworks Fast CSV reader on a 53Mb file. Looks like the line caching failed after 43,000 rows and scrambled the buffer. Tried the VB TextFieldParser and it did the trick. Thanks
          – Gone Coding
          May 21 '12 at 12:54




          +1: I just broke the lumenworks Fast CSV reader on a 53Mb file. Looks like the line caching failed after 43,000 rows and scrambled the buffer. Tried the VB TextFieldParser and it did the trick. Thanks
          – Gone Coding
          May 21 '12 at 12:54




          9




          9




          +1 Great answer, as I find many people don't know this class exists. One thing for future viewers to note is that setting parser.TextFieldType = FieldType.Delimited; is not necessary if you call parser.SetDelimiters(",");, as the method sets the TextFieldType property for you.
          – Brian
          Nov 5 '13 at 22:25





          +1 Great answer, as I find many people don't know this class exists. One thing for future viewers to note is that setting parser.TextFieldType = FieldType.Delimited; is not necessary if you call parser.SetDelimiters(",");, as the method sets the TextFieldType property for you.
          – Brian
          Nov 5 '13 at 22:25





          8




          8




          Also check this out: dotnetperls.com/textfieldparser. TextFieldParser has worse performance than String.Split and StreamReader. However, there is a big difference between string.Split and TextFieldParser. TextFieldParser handles strange cases like having a comma in a column: you can name a column like "text with quote"", and comma", and you can get the correct value text with quote", and comma instead of wrongly separated values. So you may want to opt for String.Split if you csv is very simple.
          – Yongwei Wu
          Dec 10 '13 at 3:53





          Also check this out: dotnetperls.com/textfieldparser. TextFieldParser has worse performance than String.Split and StreamReader. However, there is a big difference between string.Split and TextFieldParser. TextFieldParser handles strange cases like having a comma in a column: you can name a column like "text with quote"", and comma", and you can get the correct value text with quote", and comma instead of wrongly separated values. So you may want to opt for String.Split if you csv is very simple.
          – Yongwei Wu
          Dec 10 '13 at 3:53





          4




          4




          Note that you may need to add a reference to Microsoft.VisualBasic to use this. Right-click on your project in Visual Studio, then choose Add > Reference, and check the box for Microsoft.VisualBasic.
          – Derek Kurth
          Jan 2 '15 at 16:00




          Note that you may need to add a reference to Microsoft.VisualBasic to use this. Right-click on your project in Visual Studio, then choose Add > Reference, and check the box for Microsoft.VisualBasic.
          – Derek Kurth
          Jan 2 '15 at 16:00













          30














          My experience is that there are many different csv formats. Specially how they handle escaping of quotes and delimiters within a field.



          These are the variants I have ran into:



          • quotes are quoted and doubled (excel) i.e. 15" -> field1,"15""",field3

          • quotes are not changed unless the field is quoted for some other reason. i.e. 15" -> field1,15",fields3

          • quotes are escaped with . i.e. 15" -> field1,"15"",field3

          • quotes are not changed at all (this is not always possible to parse correctly)

          • delimiter is quoted (excel). i.e. a,b -> field1,"a,b",field3

          • delimiter is escaped with . i.e. a,b -> field1,a,b,field3

          I have tried many of the existing csv parsers but there is not a single one that can handle the variants I have ran into. It is also difficult to find out from the documentation which escaping variants the parsers support.



          In my projects I now use either the VB TextFieldParser or a custom splitter.






          share|improve this answer
















          • 1




            Love this answer for the test cases you provided!
            – Matthew Rodatus
            Oct 4 '13 at 14:32






          • 2




            The main problem is that most implementations don't care about RFC 4180 that describes the CSV format and how delimiters should be escaped.
            – Jenny O'Reilly
            May 29 '15 at 7:00















          30














          My experience is that there are many different csv formats. Specially how they handle escaping of quotes and delimiters within a field.



          These are the variants I have ran into:



          • quotes are quoted and doubled (excel) i.e. 15" -> field1,"15""",field3

          • quotes are not changed unless the field is quoted for some other reason. i.e. 15" -> field1,15",fields3

          • quotes are escaped with . i.e. 15" -> field1,"15"",field3

          • quotes are not changed at all (this is not always possible to parse correctly)

          • delimiter is quoted (excel). i.e. a,b -> field1,"a,b",field3

          • delimiter is escaped with . i.e. a,b -> field1,a,b,field3

          I have tried many of the existing csv parsers but there is not a single one that can handle the variants I have ran into. It is also difficult to find out from the documentation which escaping variants the parsers support.



          In my projects I now use either the VB TextFieldParser or a custom splitter.






          share|improve this answer
















          • 1




            Love this answer for the test cases you provided!
            – Matthew Rodatus
            Oct 4 '13 at 14:32






          • 2




            The main problem is that most implementations don't care about RFC 4180 that describes the CSV format and how delimiters should be escaped.
            – Jenny O'Reilly
            May 29 '15 at 7:00













          30












          30








          30






          My experience is that there are many different csv formats. Specially how they handle escaping of quotes and delimiters within a field.



          These are the variants I have ran into:



          • quotes are quoted and doubled (excel) i.e. 15" -> field1,"15""",field3

          • quotes are not changed unless the field is quoted for some other reason. i.e. 15" -> field1,15",fields3

          • quotes are escaped with . i.e. 15" -> field1,"15"",field3

          • quotes are not changed at all (this is not always possible to parse correctly)

          • delimiter is quoted (excel). i.e. a,b -> field1,"a,b",field3

          • delimiter is escaped with . i.e. a,b -> field1,a,b,field3

          I have tried many of the existing csv parsers but there is not a single one that can handle the variants I have ran into. It is also difficult to find out from the documentation which escaping variants the parsers support.



          In my projects I now use either the VB TextFieldParser or a custom splitter.






          share|improve this answer












          My experience is that there are many different csv formats. Specially how they handle escaping of quotes and delimiters within a field.



          These are the variants I have ran into:



          • quotes are quoted and doubled (excel) i.e. 15" -> field1,"15""",field3

          • quotes are not changed unless the field is quoted for some other reason. i.e. 15" -> field1,15",fields3

          • quotes are escaped with . i.e. 15" -> field1,"15"",field3

          • quotes are not changed at all (this is not always possible to parse correctly)

          • delimiter is quoted (excel). i.e. a,b -> field1,"a,b",field3

          • delimiter is escaped with . i.e. a,b -> field1,a,b,field3

          I have tried many of the existing csv parsers but there is not a single one that can handle the variants I have ran into. It is also difficult to find out from the documentation which escaping variants the parsers support.



          In my projects I now use either the VB TextFieldParser or a custom splitter.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 18 '10 at 9:47









          adrianmadrianm

          10.4k43976




          10.4k43976







          • 1




            Love this answer for the test cases you provided!
            – Matthew Rodatus
            Oct 4 '13 at 14:32






          • 2




            The main problem is that most implementations don't care about RFC 4180 that describes the CSV format and how delimiters should be escaped.
            – Jenny O'Reilly
            May 29 '15 at 7:00












          • 1




            Love this answer for the test cases you provided!
            – Matthew Rodatus
            Oct 4 '13 at 14:32






          • 2




            The main problem is that most implementations don't care about RFC 4180 that describes the CSV format and how delimiters should be escaped.
            – Jenny O'Reilly
            May 29 '15 at 7:00







          1




          1




          Love this answer for the test cases you provided!
          – Matthew Rodatus
          Oct 4 '13 at 14:32




          Love this answer for the test cases you provided!
          – Matthew Rodatus
          Oct 4 '13 at 14:32




          2




          2




          The main problem is that most implementations don't care about RFC 4180 that describes the CSV format and how delimiters should be escaped.
          – Jenny O'Reilly
          May 29 '15 at 7:00




          The main problem is that most implementations don't care about RFC 4180 that describes the CSV format and how delimiters should be escaped.
          – Jenny O'Reilly
          May 29 '15 at 7:00











          19














          I recommend CsvHelper from Nuget.



          (Adding a reference to Microsoft.VisualBasic just doesn't feel right, it's not only ugly, it's probably not even cross-platform.)






          share|improve this answer






















          • It's exactly as cross-platform as C# is.
            – PRMan
            Sep 6 '17 at 23:29










          • wrong, Microsoft.VisualBasic.dll in Linux comes from Mono sources, which has a different implementation than Microsoft's and there are some things that are not implemented, for example: stackoverflow.com/questions/6644165/…
            – knocte
            Sep 7 '17 at 7:59










          • (Plus, VB language has never had any focus under the companies that have been involved in creating/developing the Mono project, so it's way behind in terms of efforts, compared to the C# ecosystem/tooling.)
            – knocte
            Sep 7 '17 at 8:00






          • 1




            Having played with both, I'd add that CsvHelper comes with a built-in row to class mapper; it allows for variations in the column headers (if present), and even apparently variations in the column order (though I've not tested the latter myself). All in all it feels much more "high-level" than TextFieldParser.
            – David
            Jul 9 '18 at 15:57















          19














          I recommend CsvHelper from Nuget.



          (Adding a reference to Microsoft.VisualBasic just doesn't feel right, it's not only ugly, it's probably not even cross-platform.)






          share|improve this answer






















          • It's exactly as cross-platform as C# is.
            – PRMan
            Sep 6 '17 at 23:29










          • wrong, Microsoft.VisualBasic.dll in Linux comes from Mono sources, which has a different implementation than Microsoft's and there are some things that are not implemented, for example: stackoverflow.com/questions/6644165/…
            – knocte
            Sep 7 '17 at 7:59










          • (Plus, VB language has never had any focus under the companies that have been involved in creating/developing the Mono project, so it's way behind in terms of efforts, compared to the C# ecosystem/tooling.)
            – knocte
            Sep 7 '17 at 8:00






          • 1




            Having played with both, I'd add that CsvHelper comes with a built-in row to class mapper; it allows for variations in the column headers (if present), and even apparently variations in the column order (though I've not tested the latter myself). All in all it feels much more "high-level" than TextFieldParser.
            – David
            Jul 9 '18 at 15:57













          19












          19








          19






          I recommend CsvHelper from Nuget.



          (Adding a reference to Microsoft.VisualBasic just doesn't feel right, it's not only ugly, it's probably not even cross-platform.)






          share|improve this answer














          I recommend CsvHelper from Nuget.



          (Adding a reference to Microsoft.VisualBasic just doesn't feel right, it's not only ugly, it's probably not even cross-platform.)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 5 '16 at 18:07

























          answered Dec 10 '15 at 8:29









          knocteknocte

          9,03054788




          9,03054788











          • It's exactly as cross-platform as C# is.
            – PRMan
            Sep 6 '17 at 23:29










          • wrong, Microsoft.VisualBasic.dll in Linux comes from Mono sources, which has a different implementation than Microsoft's and there are some things that are not implemented, for example: stackoverflow.com/questions/6644165/…
            – knocte
            Sep 7 '17 at 7:59










          • (Plus, VB language has never had any focus under the companies that have been involved in creating/developing the Mono project, so it's way behind in terms of efforts, compared to the C# ecosystem/tooling.)
            – knocte
            Sep 7 '17 at 8:00






          • 1




            Having played with both, I'd add that CsvHelper comes with a built-in row to class mapper; it allows for variations in the column headers (if present), and even apparently variations in the column order (though I've not tested the latter myself). All in all it feels much more "high-level" than TextFieldParser.
            – David
            Jul 9 '18 at 15:57
















          • It's exactly as cross-platform as C# is.
            – PRMan
            Sep 6 '17 at 23:29










          • wrong, Microsoft.VisualBasic.dll in Linux comes from Mono sources, which has a different implementation than Microsoft's and there are some things that are not implemented, for example: stackoverflow.com/questions/6644165/…
            – knocte
            Sep 7 '17 at 7:59










          • (Plus, VB language has never had any focus under the companies that have been involved in creating/developing the Mono project, so it's way behind in terms of efforts, compared to the C# ecosystem/tooling.)
            – knocte
            Sep 7 '17 at 8:00






          • 1




            Having played with both, I'd add that CsvHelper comes with a built-in row to class mapper; it allows for variations in the column headers (if present), and even apparently variations in the column order (though I've not tested the latter myself). All in all it feels much more "high-level" than TextFieldParser.
            – David
            Jul 9 '18 at 15:57















          It's exactly as cross-platform as C# is.
          – PRMan
          Sep 6 '17 at 23:29




          It's exactly as cross-platform as C# is.
          – PRMan
          Sep 6 '17 at 23:29












          wrong, Microsoft.VisualBasic.dll in Linux comes from Mono sources, which has a different implementation than Microsoft's and there are some things that are not implemented, for example: stackoverflow.com/questions/6644165/…
          – knocte
          Sep 7 '17 at 7:59




          wrong, Microsoft.VisualBasic.dll in Linux comes from Mono sources, which has a different implementation than Microsoft's and there are some things that are not implemented, for example: stackoverflow.com/questions/6644165/…
          – knocte
          Sep 7 '17 at 7:59












          (Plus, VB language has never had any focus under the companies that have been involved in creating/developing the Mono project, so it's way behind in terms of efforts, compared to the C# ecosystem/tooling.)
          – knocte
          Sep 7 '17 at 8:00




          (Plus, VB language has never had any focus under the companies that have been involved in creating/developing the Mono project, so it's way behind in terms of efforts, compared to the C# ecosystem/tooling.)
          – knocte
          Sep 7 '17 at 8:00




          1




          1




          Having played with both, I'd add that CsvHelper comes with a built-in row to class mapper; it allows for variations in the column headers (if present), and even apparently variations in the column order (though I've not tested the latter myself). All in all it feels much more "high-level" than TextFieldParser.
          – David
          Jul 9 '18 at 15:57




          Having played with both, I'd add that CsvHelper comes with a built-in row to class mapper; it allows for variations in the column headers (if present), and even apparently variations in the column order (though I've not tested the latter myself). All in all it feels much more "high-level" than TextFieldParser.
          – David
          Jul 9 '18 at 15:57











          10














          Sometimes using libraries are cool when you do not want to reinvent the wheel, but in this case one can do the same job with fewer lines of code and easier to read compared to using libraries.
          Here is a different approach which I find very easy to use.



          1. In this example, I use StreamReader to read the file

          2. Regex to detect the delimiter from each line(s).

          3. An array to collect the columns from index 0 to n


          using (StreamReader reader = new StreamReader(fileName))

          string line;

          while ((line = reader.ReadLine()) != null)

          //Define pattern
          Regex CSVParser = new Regex(",(?=(?:[^"]*"[^"]*")*(?![^"]*"))");

          //Separating columns to array
          string X = CSVParser.Split(line);

          /* Do something with X */







          share|improve this answer


















          • 4




            Surely that has problems with data that itself contains new lines?
            – Doogal
            Apr 7 '16 at 14:41










          • Now CSV datafiles are not know for containing empty lines between data, but if you have a source that does that, in that case i would just simple done a simple regex test to remove whitespaces or lines containing nothing before running the reader. check here for different examples: stackoverflow.com/questions/7647716/…
            – Mana
            Apr 11 '16 at 8:38






          • 1




            Surely a char-based approach is more natural for this sort of problem than a regex. Depending on the presence of quotation marks the behavior is supposed to be different.
            – Casey
            May 9 '16 at 1:37















          10














          Sometimes using libraries are cool when you do not want to reinvent the wheel, but in this case one can do the same job with fewer lines of code and easier to read compared to using libraries.
          Here is a different approach which I find very easy to use.



          1. In this example, I use StreamReader to read the file

          2. Regex to detect the delimiter from each line(s).

          3. An array to collect the columns from index 0 to n


          using (StreamReader reader = new StreamReader(fileName))

          string line;

          while ((line = reader.ReadLine()) != null)

          //Define pattern
          Regex CSVParser = new Regex(",(?=(?:[^"]*"[^"]*")*(?![^"]*"))");

          //Separating columns to array
          string X = CSVParser.Split(line);

          /* Do something with X */







          share|improve this answer


















          • 4




            Surely that has problems with data that itself contains new lines?
            – Doogal
            Apr 7 '16 at 14:41










          • Now CSV datafiles are not know for containing empty lines between data, but if you have a source that does that, in that case i would just simple done a simple regex test to remove whitespaces or lines containing nothing before running the reader. check here for different examples: stackoverflow.com/questions/7647716/…
            – Mana
            Apr 11 '16 at 8:38






          • 1




            Surely a char-based approach is more natural for this sort of problem than a regex. Depending on the presence of quotation marks the behavior is supposed to be different.
            – Casey
            May 9 '16 at 1:37













          10












          10








          10






          Sometimes using libraries are cool when you do not want to reinvent the wheel, but in this case one can do the same job with fewer lines of code and easier to read compared to using libraries.
          Here is a different approach which I find very easy to use.



          1. In this example, I use StreamReader to read the file

          2. Regex to detect the delimiter from each line(s).

          3. An array to collect the columns from index 0 to n


          using (StreamReader reader = new StreamReader(fileName))

          string line;

          while ((line = reader.ReadLine()) != null)

          //Define pattern
          Regex CSVParser = new Regex(",(?=(?:[^"]*"[^"]*")*(?![^"]*"))");

          //Separating columns to array
          string X = CSVParser.Split(line);

          /* Do something with X */







          share|improve this answer














          Sometimes using libraries are cool when you do not want to reinvent the wheel, but in this case one can do the same job with fewer lines of code and easier to read compared to using libraries.
          Here is a different approach which I find very easy to use.



          1. In this example, I use StreamReader to read the file

          2. Regex to detect the delimiter from each line(s).

          3. An array to collect the columns from index 0 to n


          using (StreamReader reader = new StreamReader(fileName))

          string line;

          while ((line = reader.ReadLine()) != null)

          //Define pattern
          Regex CSVParser = new Regex(",(?=(?:[^"]*"[^"]*")*(?![^"]*"))");

          //Separating columns to array
          string X = CSVParser.Split(line);

          /* Do something with X */








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 23 '16 at 12:11

























          answered Dec 14 '15 at 11:25









          ManaMana

          86462651




          86462651







          • 4




            Surely that has problems with data that itself contains new lines?
            – Doogal
            Apr 7 '16 at 14:41










          • Now CSV datafiles are not know for containing empty lines between data, but if you have a source that does that, in that case i would just simple done a simple regex test to remove whitespaces or lines containing nothing before running the reader. check here for different examples: stackoverflow.com/questions/7647716/…
            – Mana
            Apr 11 '16 at 8:38






          • 1




            Surely a char-based approach is more natural for this sort of problem than a regex. Depending on the presence of quotation marks the behavior is supposed to be different.
            – Casey
            May 9 '16 at 1:37












          • 4




            Surely that has problems with data that itself contains new lines?
            – Doogal
            Apr 7 '16 at 14:41










          • Now CSV datafiles are not know for containing empty lines between data, but if you have a source that does that, in that case i would just simple done a simple regex test to remove whitespaces or lines containing nothing before running the reader. check here for different examples: stackoverflow.com/questions/7647716/…
            – Mana
            Apr 11 '16 at 8:38






          • 1




            Surely a char-based approach is more natural for this sort of problem than a regex. Depending on the presence of quotation marks the behavior is supposed to be different.
            – Casey
            May 9 '16 at 1:37







          4




          4




          Surely that has problems with data that itself contains new lines?
          – Doogal
          Apr 7 '16 at 14:41




          Surely that has problems with data that itself contains new lines?
          – Doogal
          Apr 7 '16 at 14:41












          Now CSV datafiles are not know for containing empty lines between data, but if you have a source that does that, in that case i would just simple done a simple regex test to remove whitespaces or lines containing nothing before running the reader. check here for different examples: stackoverflow.com/questions/7647716/…
          – Mana
          Apr 11 '16 at 8:38




          Now CSV datafiles are not know for containing empty lines between data, but if you have a source that does that, in that case i would just simple done a simple regex test to remove whitespaces or lines containing nothing before running the reader. check here for different examples: stackoverflow.com/questions/7647716/…
          – Mana
          Apr 11 '16 at 8:38




          1




          1




          Surely a char-based approach is more natural for this sort of problem than a regex. Depending on the presence of quotation marks the behavior is supposed to be different.
          – Casey
          May 9 '16 at 1:37




          Surely a char-based approach is more natural for this sort of problem than a regex. Depending on the presence of quotation marks the behavior is supposed to be different.
          – Casey
          May 9 '16 at 1:37











          5














          CSV can get complicated real fast.


          Use something robust and well-tested:

          FileHelpers:
          www.filehelpers.net





          The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.





          share|improve this answer


















          • 5




            I think FileHelper is trying to do to much in one go. Parsing files is a 2 step process where you first split lines into fields and then parse the fields into data. Combining the functions makes it difficult to handle things like master-detail and line filtering.
            – adrianm
            Aug 18 '10 at 10:11















          5














          CSV can get complicated real fast.


          Use something robust and well-tested:

          FileHelpers:
          www.filehelpers.net





          The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.





          share|improve this answer


















          • 5




            I think FileHelper is trying to do to much in one go. Parsing files is a 2 step process where you first split lines into fields and then parse the fields into data. Combining the functions makes it difficult to handle things like master-detail and line filtering.
            – adrianm
            Aug 18 '10 at 10:11













          5












          5








          5






          CSV can get complicated real fast.


          Use something robust and well-tested:

          FileHelpers:
          www.filehelpers.net





          The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.





          share|improve this answer














          CSV can get complicated real fast.


          Use something robust and well-tested:

          FileHelpers:
          www.filehelpers.net





          The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 31 '15 at 13:55









          MarcosMeli

          2,9701526




          2,9701526










          answered Aug 18 '10 at 7:35









          Keith BlowsKeith Blows

          1,2951012




          1,2951012







          • 5




            I think FileHelper is trying to do to much in one go. Parsing files is a 2 step process where you first split lines into fields and then parse the fields into data. Combining the functions makes it difficult to handle things like master-detail and line filtering.
            – adrianm
            Aug 18 '10 at 10:11












          • 5




            I think FileHelper is trying to do to much in one go. Parsing files is a 2 step process where you first split lines into fields and then parse the fields into data. Combining the functions makes it difficult to handle things like master-detail and line filtering.
            – adrianm
            Aug 18 '10 at 10:11







          5




          5




          I think FileHelper is trying to do to much in one go. Parsing files is a 2 step process where you first split lines into fields and then parse the fields into data. Combining the functions makes it difficult to handle things like master-detail and line filtering.
          – adrianm
          Aug 18 '10 at 10:11




          I think FileHelper is trying to do to much in one go. Parsing files is a 2 step process where you first split lines into fields and then parse the fields into data. Combining the functions makes it difficult to handle things like master-detail and line filtering.
          – adrianm
          Aug 18 '10 at 10:11











          3














          I use this here:



          http://www.codeproject.com/KB/database/GenericParser.aspx



          Last time I was looking for something like this I found it as an answer to this question.






          share|improve this answer



























            3














            I use this here:



            http://www.codeproject.com/KB/database/GenericParser.aspx



            Last time I was looking for something like this I found it as an answer to this question.






            share|improve this answer

























              3












              3








              3






              I use this here:



              http://www.codeproject.com/KB/database/GenericParser.aspx



              Last time I was looking for something like this I found it as an answer to this question.






              share|improve this answer














              I use this here:



              http://www.codeproject.com/KB/database/GenericParser.aspx



              Last time I was looking for something like this I found it as an answer to this question.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 23 '17 at 12:18









              Community

              11




              11










              answered Aug 18 '10 at 9:51









              Stefan EgliStefan Egli

              16.1k24469




              16.1k24469





















                  2














                  Another one to this list, Cinchoo ETL - an open source library to read and write CSV files



                  For a sample CSV file below



                  Id, Name
                  1, Tom
                  2, Mark


                  Quickly you can load them using library as below



                  using (var reader = new ChoCSVReader("test.csv").WithFirstLineHeader())

                  foreach (dynamic item in reader)

                  Console.WriteLine(item.Id);
                  Console.WriteLine(item.Name);




                  If you have POCO class matching the CSV file



                  public class Employee

                  public int Id get; set;
                  public string Name get; set;



                  You can use it to load the CSV file as below



                  using (var reader = new ChoCSVReader<Employee>("test.csv").WithFirstLineHeader())

                  foreach (var item in reader)

                  Console.WriteLine(item.Id);
                  Console.WriteLine(item.Name);




                  Please check out articles at CodeProject on how to use it.



                  Disclaimer: I'm the author of this library






                  share|improve this answer






















                  • Hi, can you load csv to Sql table - I dont know the header in the CSV table before hand. Just mirror whats in csv to Sql table
                    – aggie
                    Nov 15 '18 at 4:35










                  • Yes, you can. please see this link stackoverflow.com/questions/20759302/…
                    – RajN
                    Nov 15 '18 at 13:14















                  2














                  Another one to this list, Cinchoo ETL - an open source library to read and write CSV files



                  For a sample CSV file below



                  Id, Name
                  1, Tom
                  2, Mark


                  Quickly you can load them using library as below



                  using (var reader = new ChoCSVReader("test.csv").WithFirstLineHeader())

                  foreach (dynamic item in reader)

                  Console.WriteLine(item.Id);
                  Console.WriteLine(item.Name);




                  If you have POCO class matching the CSV file



                  public class Employee

                  public int Id get; set;
                  public string Name get; set;



                  You can use it to load the CSV file as below



                  using (var reader = new ChoCSVReader<Employee>("test.csv").WithFirstLineHeader())

                  foreach (var item in reader)

                  Console.WriteLine(item.Id);
                  Console.WriteLine(item.Name);




                  Please check out articles at CodeProject on how to use it.



                  Disclaimer: I'm the author of this library






                  share|improve this answer






















                  • Hi, can you load csv to Sql table - I dont know the header in the CSV table before hand. Just mirror whats in csv to Sql table
                    – aggie
                    Nov 15 '18 at 4:35










                  • Yes, you can. please see this link stackoverflow.com/questions/20759302/…
                    – RajN
                    Nov 15 '18 at 13:14













                  2












                  2








                  2






                  Another one to this list, Cinchoo ETL - an open source library to read and write CSV files



                  For a sample CSV file below



                  Id, Name
                  1, Tom
                  2, Mark


                  Quickly you can load them using library as below



                  using (var reader = new ChoCSVReader("test.csv").WithFirstLineHeader())

                  foreach (dynamic item in reader)

                  Console.WriteLine(item.Id);
                  Console.WriteLine(item.Name);




                  If you have POCO class matching the CSV file



                  public class Employee

                  public int Id get; set;
                  public string Name get; set;



                  You can use it to load the CSV file as below



                  using (var reader = new ChoCSVReader<Employee>("test.csv").WithFirstLineHeader())

                  foreach (var item in reader)

                  Console.WriteLine(item.Id);
                  Console.WriteLine(item.Name);




                  Please check out articles at CodeProject on how to use it.



                  Disclaimer: I'm the author of this library






                  share|improve this answer














                  Another one to this list, Cinchoo ETL - an open source library to read and write CSV files



                  For a sample CSV file below



                  Id, Name
                  1, Tom
                  2, Mark


                  Quickly you can load them using library as below



                  using (var reader = new ChoCSVReader("test.csv").WithFirstLineHeader())

                  foreach (dynamic item in reader)

                  Console.WriteLine(item.Id);
                  Console.WriteLine(item.Name);




                  If you have POCO class matching the CSV file



                  public class Employee

                  public int Id get; set;
                  public string Name get; set;



                  You can use it to load the CSV file as below



                  using (var reader = new ChoCSVReader<Employee>("test.csv").WithFirstLineHeader())

                  foreach (var item in reader)

                  Console.WriteLine(item.Id);
                  Console.WriteLine(item.Name);




                  Please check out articles at CodeProject on how to use it.



                  Disclaimer: I'm the author of this library







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 18 '18 at 2:34

























                  answered Nov 12 '17 at 20:13









                  RajNRajN

                  3,60521021




                  3,60521021











                  • Hi, can you load csv to Sql table - I dont know the header in the CSV table before hand. Just mirror whats in csv to Sql table
                    – aggie
                    Nov 15 '18 at 4:35










                  • Yes, you can. please see this link stackoverflow.com/questions/20759302/…
                    – RajN
                    Nov 15 '18 at 13:14
















                  • Hi, can you load csv to Sql table - I dont know the header in the CSV table before hand. Just mirror whats in csv to Sql table
                    – aggie
                    Nov 15 '18 at 4:35










                  • Yes, you can. please see this link stackoverflow.com/questions/20759302/…
                    – RajN
                    Nov 15 '18 at 13:14















                  Hi, can you load csv to Sql table - I dont know the header in the CSV table before hand. Just mirror whats in csv to Sql table
                  – aggie
                  Nov 15 '18 at 4:35




                  Hi, can you load csv to Sql table - I dont know the header in the CSV table before hand. Just mirror whats in csv to Sql table
                  – aggie
                  Nov 15 '18 at 4:35












                  Yes, you can. please see this link stackoverflow.com/questions/20759302/…
                  – RajN
                  Nov 15 '18 at 13:14




                  Yes, you can. please see this link stackoverflow.com/questions/20759302/…
                  – RajN
                  Nov 15 '18 at 13:14











                  1














                  private static DataTable ConvertCSVtoDataTable(string strFilePath)

                  DataTable dt = new DataTable();
                  using (StreamReader sr = new StreamReader(strFilePath))

                  string headers = sr.ReadLine().Split(',');
                  foreach (string header in headers)

                  dt.Columns.Add(header);

                  while (!sr.EndOfStream)

                  string rows = sr.ReadLine().Split(',');
                  DataRow dr = dt.NewRow();
                  for (int i = 0; i < headers.Length; i++)

                  dr[i] = rows[i];

                  dt.Rows.Add(dr);




                  return dt;


                  private static void WriteToDb(DataTable dt)

                  string connectionString =
                  "Data Source=localhost;" +
                  "Initial Catalog=Northwind;" +
                  "Integrated Security=SSPI;";

                  using (SqlConnection con = new SqlConnection(connectionString))

                  using (SqlCommand cmd = new SqlCommand("spInsertTest", con))

                  cmd.CommandType = CommandType.StoredProcedure;

                  cmd.Parameters.Add("@policyID", SqlDbType.Int).Value = 12;
                  cmd.Parameters.Add("@statecode", SqlDbType.VarChar).Value = "blagh2";
                  cmd.Parameters.Add("@county", SqlDbType.VarChar).Value = "blagh3";

                  con.Open();
                  cmd.ExecuteNonQuery();









                  share|improve this answer

























                    1














                    private static DataTable ConvertCSVtoDataTable(string strFilePath)

                    DataTable dt = new DataTable();
                    using (StreamReader sr = new StreamReader(strFilePath))

                    string headers = sr.ReadLine().Split(',');
                    foreach (string header in headers)

                    dt.Columns.Add(header);

                    while (!sr.EndOfStream)

                    string rows = sr.ReadLine().Split(',');
                    DataRow dr = dt.NewRow();
                    for (int i = 0; i < headers.Length; i++)

                    dr[i] = rows[i];

                    dt.Rows.Add(dr);




                    return dt;


                    private static void WriteToDb(DataTable dt)

                    string connectionString =
                    "Data Source=localhost;" +
                    "Initial Catalog=Northwind;" +
                    "Integrated Security=SSPI;";

                    using (SqlConnection con = new SqlConnection(connectionString))

                    using (SqlCommand cmd = new SqlCommand("spInsertTest", con))

                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@policyID", SqlDbType.Int).Value = 12;
                    cmd.Parameters.Add("@statecode", SqlDbType.VarChar).Value = "blagh2";
                    cmd.Parameters.Add("@county", SqlDbType.VarChar).Value = "blagh3";

                    con.Open();
                    cmd.ExecuteNonQuery();









                    share|improve this answer























                      1












                      1








                      1






                      private static DataTable ConvertCSVtoDataTable(string strFilePath)

                      DataTable dt = new DataTable();
                      using (StreamReader sr = new StreamReader(strFilePath))

                      string headers = sr.ReadLine().Split(',');
                      foreach (string header in headers)

                      dt.Columns.Add(header);

                      while (!sr.EndOfStream)

                      string rows = sr.ReadLine().Split(',');
                      DataRow dr = dt.NewRow();
                      for (int i = 0; i < headers.Length; i++)

                      dr[i] = rows[i];

                      dt.Rows.Add(dr);




                      return dt;


                      private static void WriteToDb(DataTable dt)

                      string connectionString =
                      "Data Source=localhost;" +
                      "Initial Catalog=Northwind;" +
                      "Integrated Security=SSPI;";

                      using (SqlConnection con = new SqlConnection(connectionString))

                      using (SqlCommand cmd = new SqlCommand("spInsertTest", con))

                      cmd.CommandType = CommandType.StoredProcedure;

                      cmd.Parameters.Add("@policyID", SqlDbType.Int).Value = 12;
                      cmd.Parameters.Add("@statecode", SqlDbType.VarChar).Value = "blagh2";
                      cmd.Parameters.Add("@county", SqlDbType.VarChar).Value = "blagh3";

                      con.Open();
                      cmd.ExecuteNonQuery();









                      share|improve this answer












                      private static DataTable ConvertCSVtoDataTable(string strFilePath)

                      DataTable dt = new DataTable();
                      using (StreamReader sr = new StreamReader(strFilePath))

                      string headers = sr.ReadLine().Split(',');
                      foreach (string header in headers)

                      dt.Columns.Add(header);

                      while (!sr.EndOfStream)

                      string rows = sr.ReadLine().Split(',');
                      DataRow dr = dt.NewRow();
                      for (int i = 0; i < headers.Length; i++)

                      dr[i] = rows[i];

                      dt.Rows.Add(dr);




                      return dt;


                      private static void WriteToDb(DataTable dt)

                      string connectionString =
                      "Data Source=localhost;" +
                      "Initial Catalog=Northwind;" +
                      "Integrated Security=SSPI;";

                      using (SqlConnection con = new SqlConnection(connectionString))

                      using (SqlCommand cmd = new SqlCommand("spInsertTest", con))

                      cmd.CommandType = CommandType.StoredProcedure;

                      cmd.Parameters.Add("@policyID", SqlDbType.Int).Value = 12;
                      cmd.Parameters.Add("@statecode", SqlDbType.VarChar).Value = "blagh2";
                      cmd.Parameters.Add("@county", SqlDbType.VarChar).Value = "blagh3";

                      con.Open();
                      cmd.ExecuteNonQuery();










                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 20 '17 at 14:38









                      anongf4gsdfg54564533anongf4gsdfg54564533

                      211




                      211





















                          0














                          First of all need to understand what is CSV and how to write it.



                          1. Every next string ( /r/n ) is next "table" row.

                          2. "Table" cells is separated by some delimiter symbol. Most often used symbols is t or ,

                          3. Every cell possibly can contain this delimiter symbol (cell must to start with quotes symbol and ends with this symbol in this case)

                          4. Every cell possibly can contains /r/n sybols (cell must to start with quotes symbol and ends with this symbol in this case)

                          The easiest way for C#/Visual Basic to work with CSV files is to use standard Microsoft.VisualBasic library. You just need to add needed reference, and the following string to your class:



                          using Microsoft.VisualBasic.FileIO;


                          Yes, you can use it in C#, don't worry. This library can read relatively big files and supports all of needed rules, so you will be able to work with all of CSV files.



                          Some time ago I had wrote simple class for CSV read/write based on this library. Using this simple class you will be able to work with CSV like with 2 dimensions array.
                          You can find my class by the following link:
                          https://github.com/ukushu/DataExporter



                          Simple example of using:



                          Csv csv = new Csv("t");//delimiter symbol

                          csv.FileOpen("c:\file1.csv");

                          var row1Cell6Value = csv.Rows[0][5];

                          csv.AddRow("asdf","asdffffff","5")

                          csv.FileSave("c:\file2.csv");





                          share|improve this answer



























                            0














                            First of all need to understand what is CSV and how to write it.



                            1. Every next string ( /r/n ) is next "table" row.

                            2. "Table" cells is separated by some delimiter symbol. Most often used symbols is t or ,

                            3. Every cell possibly can contain this delimiter symbol (cell must to start with quotes symbol and ends with this symbol in this case)

                            4. Every cell possibly can contains /r/n sybols (cell must to start with quotes symbol and ends with this symbol in this case)

                            The easiest way for C#/Visual Basic to work with CSV files is to use standard Microsoft.VisualBasic library. You just need to add needed reference, and the following string to your class:



                            using Microsoft.VisualBasic.FileIO;


                            Yes, you can use it in C#, don't worry. This library can read relatively big files and supports all of needed rules, so you will be able to work with all of CSV files.



                            Some time ago I had wrote simple class for CSV read/write based on this library. Using this simple class you will be able to work with CSV like with 2 dimensions array.
                            You can find my class by the following link:
                            https://github.com/ukushu/DataExporter



                            Simple example of using:



                            Csv csv = new Csv("t");//delimiter symbol

                            csv.FileOpen("c:\file1.csv");

                            var row1Cell6Value = csv.Rows[0][5];

                            csv.AddRow("asdf","asdffffff","5")

                            csv.FileSave("c:\file2.csv");





                            share|improve this answer

























                              0












                              0








                              0






                              First of all need to understand what is CSV and how to write it.



                              1. Every next string ( /r/n ) is next "table" row.

                              2. "Table" cells is separated by some delimiter symbol. Most often used symbols is t or ,

                              3. Every cell possibly can contain this delimiter symbol (cell must to start with quotes symbol and ends with this symbol in this case)

                              4. Every cell possibly can contains /r/n sybols (cell must to start with quotes symbol and ends with this symbol in this case)

                              The easiest way for C#/Visual Basic to work with CSV files is to use standard Microsoft.VisualBasic library. You just need to add needed reference, and the following string to your class:



                              using Microsoft.VisualBasic.FileIO;


                              Yes, you can use it in C#, don't worry. This library can read relatively big files and supports all of needed rules, so you will be able to work with all of CSV files.



                              Some time ago I had wrote simple class for CSV read/write based on this library. Using this simple class you will be able to work with CSV like with 2 dimensions array.
                              You can find my class by the following link:
                              https://github.com/ukushu/DataExporter



                              Simple example of using:



                              Csv csv = new Csv("t");//delimiter symbol

                              csv.FileOpen("c:\file1.csv");

                              var row1Cell6Value = csv.Rows[0][5];

                              csv.AddRow("asdf","asdffffff","5")

                              csv.FileSave("c:\file2.csv");





                              share|improve this answer














                              First of all need to understand what is CSV and how to write it.



                              1. Every next string ( /r/n ) is next "table" row.

                              2. "Table" cells is separated by some delimiter symbol. Most often used symbols is t or ,

                              3. Every cell possibly can contain this delimiter symbol (cell must to start with quotes symbol and ends with this symbol in this case)

                              4. Every cell possibly can contains /r/n sybols (cell must to start with quotes symbol and ends with this symbol in this case)

                              The easiest way for C#/Visual Basic to work with CSV files is to use standard Microsoft.VisualBasic library. You just need to add needed reference, and the following string to your class:



                              using Microsoft.VisualBasic.FileIO;


                              Yes, you can use it in C#, don't worry. This library can read relatively big files and supports all of needed rules, so you will be able to work with all of CSV files.



                              Some time ago I had wrote simple class for CSV read/write based on this library. Using this simple class you will be able to work with CSV like with 2 dimensions array.
                              You can find my class by the following link:
                              https://github.com/ukushu/DataExporter



                              Simple example of using:



                              Csv csv = new Csv("t");//delimiter symbol

                              csv.FileOpen("c:\file1.csv");

                              var row1Cell6Value = csv.Rows[0][5];

                              csv.AddRow("asdf","asdffffff","5")

                              csv.FileSave("c:\file2.csv");






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Apr 1 '17 at 4:40

























                              answered Apr 1 '17 at 4:13









                              AndrewAndrew

                              2,03611223




                              2,03611223





















                                  0














                                  To complete the previous answers, one may need a collection of objects from his CSV File, either parsed by the TextFieldParser or the string.Split method, and then each line converted to an object via Reflection. You obviously first need to define a class that matches the lines of the CSV file.



                                  I used the simple CSV Serializer from Michael Kropat found here: Generic class to CSV (all properties)
                                  and reused his methods to get the fields and properties of the wished class.



                                  I deserialize my CSV file with the following method:



                                  public static IEnumerable<T> ReadCsvFileTextFieldParser<T>(string fileFullPath, string delimiter = ";") where T : new()

                                  if (!File.Exists(fileFullPath))

                                  return null;


                                  var list = new List<T>();
                                  var csvFields = GetAllFieldOfClass<T>();
                                  var fieldDict = new Dictionary<int, MemberInfo>();

                                  using (TextFieldParser parser = new TextFieldParser(fileFullPath))

                                  parser.SetDelimiters(delimiter);

                                  bool headerParsed = false;

                                  while (!parser.EndOfData)

                                  //Processing row
                                  string rowFields = parser.ReadFields();
                                  if (!headerParsed)

                                  for (int i = 0; i < rowFields.Length; i++)

                                  // First row shall be the header!
                                  var csvField = csvFields.Where(f => f.Name == rowFields[i]).FirstOrDefault();
                                  if (csvField != null)

                                  fieldDict.Add(i, csvField);


                                  headerParsed = true;

                                  else

                                  T newObj = new T();
                                  for (int i = 0; i < rowFields.Length; i++)

                                  var csvFied = fieldDict[i];
                                  var record = rowFields[i];

                                  if (csvFied is FieldInfo)

                                  ((FieldInfo)csvFied).SetValue(newObj, record);

                                  else if (csvFied is PropertyInfo)

                                  var pi = (PropertyInfo)csvFied;
                                  pi.SetValue(newObj, Convert.ChangeType(record, pi.PropertyType), null);

                                  else

                                  throw new Exception("Unhandled case.");


                                  if (newObj != null)

                                  list.Add(newObj);




                                  return list;


                                  public static IEnumerable<MemberInfo> GetAllFieldOfClass<T>()

                                  return
                                  from mi in typeof(T).GetMembers(BindingFlags.Public





                                  share|improve this answer



























                                    0














                                    To complete the previous answers, one may need a collection of objects from his CSV File, either parsed by the TextFieldParser or the string.Split method, and then each line converted to an object via Reflection. You obviously first need to define a class that matches the lines of the CSV file.



                                    I used the simple CSV Serializer from Michael Kropat found here: Generic class to CSV (all properties)
                                    and reused his methods to get the fields and properties of the wished class.



                                    I deserialize my CSV file with the following method:



                                    public static IEnumerable<T> ReadCsvFileTextFieldParser<T>(string fileFullPath, string delimiter = ";") where T : new()

                                    if (!File.Exists(fileFullPath))

                                    return null;


                                    var list = new List<T>();
                                    var csvFields = GetAllFieldOfClass<T>();
                                    var fieldDict = new Dictionary<int, MemberInfo>();

                                    using (TextFieldParser parser = new TextFieldParser(fileFullPath))

                                    parser.SetDelimiters(delimiter);

                                    bool headerParsed = false;

                                    while (!parser.EndOfData)

                                    //Processing row
                                    string rowFields = parser.ReadFields();
                                    if (!headerParsed)

                                    for (int i = 0; i < rowFields.Length; i++)

                                    // First row shall be the header!
                                    var csvField = csvFields.Where(f => f.Name == rowFields[i]).FirstOrDefault();
                                    if (csvField != null)

                                    fieldDict.Add(i, csvField);


                                    headerParsed = true;

                                    else

                                    T newObj = new T();
                                    for (int i = 0; i < rowFields.Length; i++)

                                    var csvFied = fieldDict[i];
                                    var record = rowFields[i];

                                    if (csvFied is FieldInfo)

                                    ((FieldInfo)csvFied).SetValue(newObj, record);

                                    else if (csvFied is PropertyInfo)

                                    var pi = (PropertyInfo)csvFied;
                                    pi.SetValue(newObj, Convert.ChangeType(record, pi.PropertyType), null);

                                    else

                                    throw new Exception("Unhandled case.");


                                    if (newObj != null)

                                    list.Add(newObj);




                                    return list;


                                    public static IEnumerable<MemberInfo> GetAllFieldOfClass<T>()

                                    return
                                    from mi in typeof(T).GetMembers(BindingFlags.Public





                                    share|improve this answer

























                                      0












                                      0








                                      0






                                      To complete the previous answers, one may need a collection of objects from his CSV File, either parsed by the TextFieldParser or the string.Split method, and then each line converted to an object via Reflection. You obviously first need to define a class that matches the lines of the CSV file.



                                      I used the simple CSV Serializer from Michael Kropat found here: Generic class to CSV (all properties)
                                      and reused his methods to get the fields and properties of the wished class.



                                      I deserialize my CSV file with the following method:



                                      public static IEnumerable<T> ReadCsvFileTextFieldParser<T>(string fileFullPath, string delimiter = ";") where T : new()

                                      if (!File.Exists(fileFullPath))

                                      return null;


                                      var list = new List<T>();
                                      var csvFields = GetAllFieldOfClass<T>();
                                      var fieldDict = new Dictionary<int, MemberInfo>();

                                      using (TextFieldParser parser = new TextFieldParser(fileFullPath))

                                      parser.SetDelimiters(delimiter);

                                      bool headerParsed = false;

                                      while (!parser.EndOfData)

                                      //Processing row
                                      string rowFields = parser.ReadFields();
                                      if (!headerParsed)

                                      for (int i = 0; i < rowFields.Length; i++)

                                      // First row shall be the header!
                                      var csvField = csvFields.Where(f => f.Name == rowFields[i]).FirstOrDefault();
                                      if (csvField != null)

                                      fieldDict.Add(i, csvField);


                                      headerParsed = true;

                                      else

                                      T newObj = new T();
                                      for (int i = 0; i < rowFields.Length; i++)

                                      var csvFied = fieldDict[i];
                                      var record = rowFields[i];

                                      if (csvFied is FieldInfo)

                                      ((FieldInfo)csvFied).SetValue(newObj, record);

                                      else if (csvFied is PropertyInfo)

                                      var pi = (PropertyInfo)csvFied;
                                      pi.SetValue(newObj, Convert.ChangeType(record, pi.PropertyType), null);

                                      else

                                      throw new Exception("Unhandled case.");


                                      if (newObj != null)

                                      list.Add(newObj);




                                      return list;


                                      public static IEnumerable<MemberInfo> GetAllFieldOfClass<T>()

                                      return
                                      from mi in typeof(T).GetMembers(BindingFlags.Public





                                      share|improve this answer














                                      To complete the previous answers, one may need a collection of objects from his CSV File, either parsed by the TextFieldParser or the string.Split method, and then each line converted to an object via Reflection. You obviously first need to define a class that matches the lines of the CSV file.



                                      I used the simple CSV Serializer from Michael Kropat found here: Generic class to CSV (all properties)
                                      and reused his methods to get the fields and properties of the wished class.



                                      I deserialize my CSV file with the following method:



                                      public static IEnumerable<T> ReadCsvFileTextFieldParser<T>(string fileFullPath, string delimiter = ";") where T : new()

                                      if (!File.Exists(fileFullPath))

                                      return null;


                                      var list = new List<T>();
                                      var csvFields = GetAllFieldOfClass<T>();
                                      var fieldDict = new Dictionary<int, MemberInfo>();

                                      using (TextFieldParser parser = new TextFieldParser(fileFullPath))

                                      parser.SetDelimiters(delimiter);

                                      bool headerParsed = false;

                                      while (!parser.EndOfData)

                                      //Processing row
                                      string rowFields = parser.ReadFields();
                                      if (!headerParsed)

                                      for (int i = 0; i < rowFields.Length; i++)

                                      // First row shall be the header!
                                      var csvField = csvFields.Where(f => f.Name == rowFields[i]).FirstOrDefault();
                                      if (csvField != null)

                                      fieldDict.Add(i, csvField);


                                      headerParsed = true;

                                      else

                                      T newObj = new T();
                                      for (int i = 0; i < rowFields.Length; i++)

                                      var csvFied = fieldDict[i];
                                      var record = rowFields[i];

                                      if (csvFied is FieldInfo)

                                      ((FieldInfo)csvFied).SetValue(newObj, record);

                                      else if (csvFied is PropertyInfo)

                                      var pi = (PropertyInfo)csvFied;
                                      pi.SetValue(newObj, Convert.ChangeType(record, pi.PropertyType), null);

                                      else

                                      throw new Exception("Unhandled case.");


                                      if (newObj != null)

                                      list.Add(newObj);




                                      return list;


                                      public static IEnumerable<MemberInfo> GetAllFieldOfClass<T>()

                                      return
                                      from mi in typeof(T).GetMembers(BindingFlags.Public






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited May 23 '17 at 12:10









                                      Community

                                      11




                                      11










                                      answered Sep 6 '16 at 8:42









                                      EricBDevEricBDev

                                      32127




                                      32127



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3507498%2freading-csv-files-using-c-sharp%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Use pre created SQLite database for Android project in kotlin

                                          Darth Vader #20

                                          Ondo