Title: Use a TextFieldParser to read delimited data in C#
This example uses a TextFieldParser object to parse a data file that contains name and address data. The data contains fields delimited by commas and semi-colons. Some records also have ZIP+4 format ZIP codes (for example, 08109-2120) and the program should discard the +4 part of the data. Finally the data file contains some blank lines in the middle and at the end. The program should ignore them.
The following text shows the test data file.
Barbara,Roberts;2044 22nd St;San Pablo;CA;24806
Ken,Carson;565 Atlanta South Pkwy;Atlanta;GA;10349
Horatio,Crunch;565 Atlanta South Pkwy;Atlanta;GA;70349-5958
Patricia,Reichardt;3655 Millbranch Rd;Memphis;TN;18116-4817
Aloysius,Snuffleupagus;9252 N 49th St;Pennsauken;NJ;08109-2120
Edgar,Mallory;3655 Millbranch Rd;Memphis;TN;38116
Jonas,Grumby;2832 Amerson Way;Ellenwood;GA;30294
Roy,Hinkley;29426 Christiana Way;Laguna Niguel;CA;92677
The example program uses the following code to read and display the data.
// Open a TextFieldParser using these delimiters.
string[] delimiters = { ";", ",", "-" };
using (TextFieldParser parser =
FileSystem.OpenTextFieldParser("Names.txt", delimiters))
{
// Process the file's lines.
while (!parser.EndOfData)
{
try
{
string[] fields = parser.ReadFields();
ListViewItem item = lvwPeople.Items.Add(fields[0]);
for (int i = 1; i <= 5; i++)
{
item.SubItems.Add(fields[i]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
The code starts by creating an array holding the delimiters that the file uses. Including - in the list allows the program to treat the +4 part of the ZIP code (if it is present) as a separate field.
The program then creates a TextFieldParser object. It uses the FileSystem.OpenTextFieldParser factory method to create a new instance of the class. It passes that method the array of delimiters so the parser knows what characters to use as delimiters. Notice that the code uses a using statement so the parser's Dispose method is automatically called when the object is no longer needed.
Note also that the TextFieldParser class and the FileSystem class are contained in the Microsoft.VisualBasic.FileIO namespace. The program includes the following using directive to make using the namespace easier. I also had to add a reference to the Microsoft.VisualBasic library at design time.
// Add a reference to Microsoft.VisualBasic.
using Microsoft.VisualBasic.FileIO;
After creating the parser, the program loops while the parser's EndOfData property returns false. The EndOfData property automatically changes to true after the parser reads the last record in the data file even if the file ends with some blank lines so the parser won't get confused.
As long as EndOfData is false, the program calls the parser's ReadFields method to read the next record's fields into an array of strings. The program must then interpret the fields appropriately. In this example the program simply displays the first 6 fields in a ListView control, one as a ListView item and the others as sub-items.
This is nothing that you couldn't do yourself by using File.ReadAllLines and String.Split, but the TextFieldParser is somewhat simpler.
Download the example to experiment with it and to see additional details.
|