Export (0) Print
Expand All

TextFieldParser Class

Provides methods and properties for parsing structured text files.

System.Object
  Microsoft.VisualBasic.FileIO.TextFieldParser

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

public class TextFieldParser : IDisposable

The TextFieldParser type exposes the following members.

  NameDescription
Public methodTextFieldParser(Stream)Initializes a new instance of the TextFieldParser class.
Public methodTextFieldParser(String)Initializes a new instance of the TextFieldParser class.
Public methodTextFieldParser(TextReader)Initializes a new instance of the TextFieldParser class.
Public methodTextFieldParser(Stream, Encoding)Initializes a new instance of the TextFieldParser class.
Public methodTextFieldParser(String, Encoding)Initializes a new instance of the TextFieldParser class.
Public methodTextFieldParser(Stream, Encoding, Boolean)Initializes a new instance of the TextFieldParser class.
Public methodTextFieldParser(String, Encoding, Boolean)Initializes a new instance of the TextFieldParser class.
Public methodTextFieldParser(Stream, Encoding, Boolean, Boolean)Initializes a new instance of the TextFieldParser class.
Top

  NameDescription
Public propertyCommentTokensDefines 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.
Public propertyDelimitersDefines the delimiters for a text file.
Public propertyEndOfDataReturns True if there are no non-blank, non-comment lines between the current cursor position and the end of the file.
Public propertyErrorLineReturns the line that caused the most recent MalformedLineException exception.
Public propertyErrorLineNumberReturns the number of the line that caused the most recent MalformedLineException exception.
Public propertyFieldWidthsDenotes the width of each column in the text file being parsed.
Public propertyHasFieldsEnclosedInQuotesDenotes whether fields are enclosed in quotation marks when a delimited file is being parsed.
Public propertyLineNumberReturns the current line number, or returns -1 if no more characters are available in the stream.
Public propertyTextFieldTypeIndicates whether the file to be parsed is delimited or fixed-width.
Public propertyTrimWhiteSpaceIndicates whether leading and trailing white space should be trimmed from field values.
Top

  NameDescription
Public methodCloseCloses the current TextFieldParser object.
Public methodDispose()Releases resources used by the TextFieldParser object.
Protected methodDispose(Boolean)Releases resources used by the TextFieldParser object.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows the TextFieldParser object to attempt to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Overrides Object.Finalize().)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodPeekCharsReads the specified number of characters without advancing the cursor.
Public methodReadFieldsReads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.
Public methodReadLineReturns the current line as a string and advances the cursor to the next line.
Public methodReadToEndReads the remainder of the text file and returns it as a string.
Public methodSetDelimitersSets the delimiters for the reader to the specified values, and sets the field type to Delimited.
Public methodSetFieldWidthsSets the delimiters for the reader to the specified values.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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