TextFieldParser TextFieldParser TextFieldParser TextFieldParser Class

Definition

Provides methods and properties for parsing structured text files.

public ref class TextFieldParser : IDisposable
public class TextFieldParser : IDisposable
type TextFieldParser = class
    interface IDisposable
Public Class TextFieldParser
Implements IDisposable
Inheritance
TextFieldParserTextFieldParserTextFieldParserTextFieldParser
Implements

Examples

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

Remarks

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
Read from a fixed-width text file How to: Read From Fixed-width Text Files
Read from a text file with multiple formats How to: Read From Text Files with Multiple Formats

Constructors

TextFieldParser(Stream) TextFieldParser(Stream) TextFieldParser(Stream) TextFieldParser(Stream)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding) TextFieldParser(Stream, Encoding) TextFieldParser(Stream, Encoding) TextFieldParser(Stream, Encoding)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding, Boolean) TextFieldParser(Stream, Encoding, Boolean) TextFieldParser(Stream, Encoding, Boolean) TextFieldParser(Stream, Encoding, Boolean)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(Stream, Encoding, Boolean, Boolean) TextFieldParser(Stream, Encoding, Boolean, Boolean) TextFieldParser(Stream, Encoding, Boolean, Boolean) TextFieldParser(Stream, Encoding, Boolean, Boolean)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(String) TextFieldParser(String) TextFieldParser(String) TextFieldParser(String)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(String, Encoding) TextFieldParser(String, Encoding) TextFieldParser(String, Encoding) TextFieldParser(String, Encoding)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(String, Encoding, Boolean) TextFieldParser(String, Encoding, Boolean) TextFieldParser(String, Encoding, Boolean) TextFieldParser(String, Encoding, Boolean)

Initializes a new instance of the TextFieldParser class.

TextFieldParser(TextReader) TextFieldParser(TextReader) TextFieldParser(TextReader) TextFieldParser(TextReader)

Initializes a new instance of the TextFieldParser class.

Properties

CommentTokens CommentTokens CommentTokens 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 Delimiters Delimiters Delimiters

Defines the delimiters for a text file.

EndOfData EndOfData EndOfData EndOfData

Returns True if there are no non-blank, non-comment lines between the current cursor position and the end of the file.

ErrorLine ErrorLine ErrorLine ErrorLine

Returns the line that caused the most recent MalformedLineException exception.

ErrorLineNumber ErrorLineNumber ErrorLineNumber ErrorLineNumber

Returns the number of the line that caused the most recent MalformedLineException exception.

FieldWidths FieldWidths FieldWidths FieldWidths

Denotes the width of each column in the text file being parsed.

HasFieldsEnclosedInQuotes HasFieldsEnclosedInQuotes HasFieldsEnclosedInQuotes HasFieldsEnclosedInQuotes

Denotes whether fields are enclosed in quotation marks when a delimited file is being parsed.

LineNumber LineNumber LineNumber LineNumber

Returns the current line number, or returns -1 if no more characters are available in the stream.

TextFieldType TextFieldType TextFieldType TextFieldType

Indicates whether the file to be parsed is delimited or fixed-width.

TrimWhiteSpace TrimWhiteSpace TrimWhiteSpace TrimWhiteSpace

Indicates whether leading and trailing white space should be trimmed from field values.

Methods

Close() Close() Close() Close()

Closes the current TextFieldParser object.

Dispose() Dispose() Dispose() Dispose()

Releases resources used by the TextFieldParser object.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Releases resources used by the TextFieldParser object.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

Allows the TextFieldParser object to attempt to free resources and perform other cleanup operations before it is reclaimed by garbage collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
PeekChars(Int32) PeekChars(Int32) PeekChars(Int32) PeekChars(Int32)

Reads the specified number of characters without advancing the cursor.

ReadFields() ReadFields() ReadFields() 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() ReadLine() ReadLine() ReadLine()

Returns the current line as a string and advances the cursor to the next line.

ReadToEnd() ReadToEnd() ReadToEnd() ReadToEnd()

Reads the remainder of the text file and returns it as a string.

SetDelimiters(String[]) SetDelimiters(String[]) SetDelimiters(String[]) SetDelimiters(String[])

Sets the delimiters for the reader to the specified values, and sets the field type to Delimited.

SetFieldWidths(Int32[]) SetFieldWidths(Int32[]) SetFieldWidths(Int32[]) SetFieldWidths(Int32[])

Sets the delimiters for the reader to the specified values.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See Also