Parsing Text Files with the TextFieldParser Object
Updated: July 2008
The TextFieldParser object allows you to parse and process very large file that are structured as delimited-width columns of text, such as log files or legacy database information. Parsing a text file with TextFieldParser is similar to iterating over a text file, while the parse method to extract fields of text is similar to string manipulation methods used to tokenize delimited strings.
Text files may have fields of various width, delimited by a character such as a comma or a tab space. Define TextFieldType and the delimiter, as in the following example, which uses the SetDelimiters method to define a tab-delimited text file:
Other text files may have field widths that are fixed. In such cases, you need to define the TextFieldType as FixedWidth and define the widths of each field, as in the following example. This example uses the SetFieldWidths method to define the columns of text: the first column is 5 characters wide, the second is 10, the third is 11, and the fourth is of variable width.
Once the format is defined, you can loop through the file, using the ReadFields method to process each line in turn.
If a field does not match the specified format, a MalformedLineException exception is thrown. When such exceptions are thrown, the ErrorLine and ErrorLineNumber properties hold the text causing the exception and the line number of that text.
The PeekChars method of the TextFieldParser object can be used to check each field before reading it, allowing you to define multiple formats for the fields and react accordingly. For more information, see How to: Read From Text Files with Multiple Formats in Visual Basic.