TextFieldParser Class

 

Provides methods and properties for parsing structured text files.

Namespace:   Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

System.Object
  Microsoft.VisualBasic.FileIO.TextFieldParser

public class TextFieldParser : IDisposable

NameDescription
System_CAPS_pubmethodTextFieldParser(Stream)

Initializes a new instance of the TextFieldParser class.

System_CAPS_pubmethodTextFieldParser(Stream, Encoding)

Initializes a new instance of the TextFieldParser class.

System_CAPS_pubmethodTextFieldParser(Stream, Encoding, Boolean)

Initializes a new instance of the TextFieldParser class.

System_CAPS_pubmethodTextFieldParser(Stream, Encoding, Boolean, Boolean)

Initializes a new instance of the TextFieldParser class.

System_CAPS_pubmethodTextFieldParser(String)

Initializes a new instance of the TextFieldParser class.

System_CAPS_pubmethodTextFieldParser(String, Encoding)

Initializes a new instance of the TextFieldParser class.

System_CAPS_pubmethodTextFieldParser(String, Encoding, Boolean)

Initializes a new instance of the TextFieldParser class.

System_CAPS_pubmethodTextFieldParser(TextReader)

Initializes a new instance of the TextFieldParser class.

NameDescription
System_CAPS_pubpropertyCommentTokens

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.

System_CAPS_pubpropertyDelimiters

Defines the delimiters for a text file.

System_CAPS_pubpropertyEndOfData

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

System_CAPS_pubpropertyErrorLine

Returns the line that caused the most recent MalformedLineException exception.

System_CAPS_pubpropertyErrorLineNumber

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

System_CAPS_pubpropertyFieldWidths

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

System_CAPS_pubpropertyHasFieldsEnclosedInQuotes

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

System_CAPS_pubpropertyLineNumber

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

System_CAPS_pubpropertyTextFieldType

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

System_CAPS_pubpropertyTrimWhiteSpace

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

NameDescription
System_CAPS_pubmethodClose()

Closes the current TextFieldParser object.

System_CAPS_pubmethodDispose()

Releases resources used by the TextFieldParser object.

System_CAPS_protmethodDispose(Boolean)

Releases resources used by the TextFieldParser object.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

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

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodPeekChars(Int32)

Reads the specified number of characters without advancing the cursor.

System_CAPS_pubmethodReadFields()

Reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.

System_CAPS_pubmethodReadLine()

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

System_CAPS_pubmethodReadToEnd()

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

System_CAPS_pubmethodSetDelimiters(String[])

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

System_CAPS_pubmethodSetFieldWidths(Int32[])

Sets the delimiters for the reader to the specified values.

System_CAPS_pubmethodToString()

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

How to: Read From Fixed-width Text Files in Visual Basic

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

.NET Framework
Available since 2.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: