Home
Map
TextFieldParser Examples: Read CSVUse TextFieldParser to read CSV files. Use the Microsoft.VisualBasic.FileIO namespace.
C#
This page was last reviewed on May 22, 2021.
TextFieldParser. This class reads in CSV files. With it, we specify a delimiter string, and then can read in the fields of every line in a loop.
A simpler approach. We can use the TextFieldParser instead of the string.Split C# method. We demonstrate and benchmark the TextFieldParser.
File
Example. With TextFieldParser, we must assign the Delimiters property to a string array. The delimiters tell the parser where field send and new fields begin on a single line.
Further We show the ReadFields method, which returns null when the end of file condition is met.
And In the result, the array returned contains every field in its own element. We display the length.
using System; using Microsoft.VisualBasic.FileIO; class Program { static void Main() { using (TextFieldParser parser = new TextFieldParser("C:\\csv.txt")) { parser.Delimiters = new string[] { "," }; while (true) { string[] parts = parser.ReadFields(); if (parts == null) { break; } Console.WriteLine("{0} field(s)", parts.Length); } } } }
a,b,c d,e,f gh,ij
3 field(s) 3 field(s) 2 field(s)
Benchmark. Next I benchmark for the TextFieldParser and string.Split. The code only uses a single char delimiter (the comma), and it only tests 3-line and 300-line files.
Info The results are convincing to me that the TextFieldParser cannot be taken seriously for performance work.
Result The string.Split method is many times faster at populating arrays. The TextFieldParser seems to scale worse.
using System; using System.Diagnostics; using System.IO; using Microsoft.VisualBasic.FileIO; class Program { const int _max = 10000; static void Main() { Method1(); Method2(); System.Threading.Thread.Sleep(1000); var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { Method1(); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { Method2(); } s2.Stop(); Console.WriteLine(s1.Elapsed.TotalMilliseconds); Console.WriteLine(s2.Elapsed.TotalMilliseconds); } static void Method1() { using (TextFieldParser parser = new TextFieldParser("C:\\csv.txt")) { parser.Delimiters = new string[] { "," }; while (true) { string[] parts = parser.ReadFields(); if (parts == null) { break; } // Console.WriteLine("{0} field(s)", parts.Length); } } } static void Method2() { char[] delimiters = new char[] { ',' }; using (StreamReader reader = new StreamReader("C:\\csv.txt")) { while (true) { string line = reader.ReadLine(); if (line == null) { break; } string[] parts = line.Split(delimiters); // Console.WriteLine("{0} field(s)", parts.Length); } } } }
2616 ms 623 ms
10762 ms 1186 ms
Add reference note. To access TextFieldParser, go to Add Reference, and select Microsoft.VisualBasic. In .NET 5, this is not necessary.
A summary. We looked at the TextFieldParser type from the Microsoft.VisualBasic.FileIO namespace in the C# language. We saw how the ReadFields method can be used.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 22, 2021 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.