TextFieldParser Class
Provides methods and properties for parsing structured text files.
Assembly: Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
The TextFieldParser type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | TextFieldParser(Stream) | Initializes a new instance of the TextFieldParser class. |
![]() | TextFieldParser(String) | Initializes a new instance of the TextFieldParser class. |
![]() | TextFieldParser(TextReader) | Initializes a new instance of the TextFieldParser class. |
![]() | TextFieldParser(Stream, Encoding) | Initializes a new instance of the TextFieldParser class. |
![]() | TextFieldParser(String, Encoding) | Initializes a new instance of the TextFieldParser class. |
![]() | TextFieldParser(Stream, Encoding, Boolean) | Initializes a new instance of the TextFieldParser class. |
![]() | TextFieldParser(String, Encoding, Boolean) | Initializes a new instance of the TextFieldParser class. |
![]() | TextFieldParser(Stream, Encoding, Boolean, Boolean) | Initializes a new instance of the TextFieldParser class. |
| Name | Description | |
|---|---|---|
![]() | CommentTokens | Defines comment tokens. A comment token is a string that, when placed at the beginning of a line, indicates that the line is a comment and should be ignored by the parser. |
![]() | Delimiters | Defines the delimiters for a text file. |
![]() | EndOfData | Returns True if there are no non-blank, non-comment lines between the current cursor position and the end of the file. |
![]() | ErrorLine | Returns the line that caused the most recent MalformedLineException exception. |
![]() | ErrorLineNumber | Returns the number of the line that caused the most recent MalformedLineException exception. |
![]() | FieldWidths | Denotes the width of each column in the text file being parsed. |
![]() | HasFieldsEnclosedInQuotes | Denotes whether fields are enclosed in quotation marks when a delimited file is being parsed. |
![]() | LineNumber | Returns the current line number, or returns -1 if no more characters are available in the stream. |
![]() | TextFieldType | Indicates whether the file to be parsed is delimited or fixed-width. |
![]() | TrimWhiteSpace | Indicates whether leading and trailing white space should be trimmed from field values. |
| Name | Description | |
|---|---|---|
![]() | Close | Closes the current TextFieldParser object. |
![]() | Dispose() | Releases resources used by the TextFieldParser object. |
![]() | Dispose(Boolean) | Releases resources used by the TextFieldParser object. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows the TextFieldParser object to attempt to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Overrides Object::Finalize().) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | PeekChars | Reads the specified number of characters without advancing the cursor. |
![]() | ReadFields | Reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data. |
![]() | ReadLine | Returns the current line as a string and advances the cursor to the next line. |
![]() | ReadToEnd | Reads the remainder of the text file and returns it as a string. |
![]() | SetDelimiters | Sets the delimiters for the reader to the specified values, and sets the field type to Delimited. |
![]() | SetFieldWidths | Sets the delimiters for the reader to the specified values. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
The TextFieldParser object provides methods and properties for parsing structured text files. Parsing a text file with the TextFieldParser is similar to iterating over a text file, while the ReadFields method to extract fields of text is similar to splitting the strings.
The TextFieldParser can parse two types of files: delimited or fixed-width. Some properties, such as Delimiters and HasFieldsEnclosedInQuotes are meaningful only when working with delimited files, while the FieldWidths property is meaningful only when working with fixed-width files.
The following table lists examples of tasks involving the Microsoft.VisualBasic.FileIO.TextFieldParser object.
To | See |
|---|---|
Read from a delimited text file | How to: Read From Comma-Delimited Text Files in Visual Basic |
Read from a fixed-width text file | |
Read from a text file with multiple formats | How to: Read From Text Files with Multiple Formats in Visual Basic |
This example parses through a tab-delimited text file, Bigfile.
Using MyReader As New Microsoft.VisualBasic.FileIO. TextFieldParser("c:\logs\bigfile") MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited MyReader.Delimiters = New String() {vbTab} Dim currentRow As String() 'Loop through all of the fields in the file. 'If any lines are corrupt, report an error and continue parsing. While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() ' Include code here to handle the row. Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException MsgBox("Line " & ex.Message & " is invalid. Skipping") End Try End While End Using
This example depends on the existence of a function, processFields, which processes the fields as they are read.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
