In the former version of ADO.NET we can use regular expression to read a text file which delimited by a (some) character(s) or fixed-length fields. The new TextFieldParser type in VB 2005 offer a simpler way to do the same task. If we consider the namespace, the type is not belonging to the "official" .net framework. C# developer still can use it by adding the reference to that namespace.
To begin with we have to create TextFieldParser object by passing the filename to its constructor. To obtain the delimiter character you assign the Delimiter property to the array of string. If we want to discard the space, set TrimWhiteSpace property to true. Next we need to loop the text on the file line by line until EndOfData property returns true.
To read the value from each field in the delimited text, TextFieldParser give us a method named ReadFields which returns array of string value, so we have to create an array of string variable to catch those value. Let say i have a file named "books1.txt". The value is delimited by comma character for separating between values (field). Second file is "books2.txt". This file has a fixed-length char between value field. For briefly, i have created a simple windows application to see how it so simple to read such text file. Each file created automatically when the form is load. Let us consider the following code :
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Public Class frmTextFieldParser
Private Sub frmTextFieldParser_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim sw As StreamWriter
'================creating comma delimited text file=============
sw = New StreamWriter("C:\books1.txt", False)
sw.WriteLine(String.Format("{0},{1}", "VB 2005", "Que"))
sw.WriteLine(String.Format("{0},{1}", "C# 2005", "Apress"))
sw.WriteLine(String.Format("{0},{1}", "C++ 2005", _
"MS Press"))
sw.Flush()
sw.Close()
'=================creating fixed-length char text file===========
Dim listOfTitle As New List(Of String)
listOfTitle.AddRange(New String() {"VB 2005", "C# 2005", _
"C++ 2005"})
'//15 fixed-length char title
Const constMaxLengthTitle As Integer = 15
'//the difference length between title and max length
'//for separating value between fields
Dim intTitleDiffLength(2) As Integer
Dim intCounter As Integer = 0
For Each aTitle As String In listOfTitle
Dim intTitleLength As Integer = aTitle.Length
intTitleDiffLength(intCounter) = constMaxLengthTitle - _
intTitleLength
intCounter += 1
Next
Dim listOfPublishers As New List(Of String)
listOfPublishers.AddRange(New String() {"Que", "Apress", _
"MS Press"})
'//10 fixed-length char publisher
Const constPublisherLength As Integer = 10
'//the difference length between publisher and max length
'//for separating value between fields
Dim intPubDiffLength(2) As Integer
intCounter = 0
For Each aPublisher As String In listOfPublishers
Dim intPublisherLength As Integer = aPublisher.Length
intPubDiffLength(intCounter) = constPublisherLength - _
intPublisherLength
intCounter += 1
Next
'//loop all the element of title and publisher
'//to write its value to the text file
sw = New StreamWriter("C:\books2.txt", False)
For intRec As Integer = 0 To listOfTitle.Count - 1
'//space between field is created automatically
'//based on the difference length between fields value
'//and allowed max length
sw.WriteLine(String.Format("{0}{1}", listOfTitle(intRec) & _
New String(" ", intTitleDiffLength(intRec)), _
listOfPublishers(intRec) & New String(" ", _
intPubDiffLength(intRec))))
Next
sw.Flush()
sw.Close()
End Sub
Private Sub btnDelimited_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDelimited.Click
Using txtFieldParser As New TextFieldParser("C:\books1.txt")
'//assign the TextFieldType property value
'//to the FieldType.Delimited enumeration value
txtFieldParser.TextFieldType = FieldType.Delimited
'//obtaining the delimiter char
txtFieldParser.Delimiters = New String() {","}
'//discard the space (if we wanted to)
txtFieldParser.TrimWhiteSpace = True
Do Until txtFieldParser.EndOfData
'//array of string to read the field value
Dim fields() As String = txtFieldParser.ReadFields
Console.WriteLine(String.Format("{0} {1}", fields(0), _
fields(1)))
Loop
End Using
End Sub
Private Sub btnFixedLength_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnFixedLength.Click
Using txtFieldParser As New TextFieldParser("C:\books2.txt")
'//assign the TextFieldType property value
'//to the FieldType.FixedWidth enumeration value
txtFieldParser.TextFieldType = FieldType.FixedWidth
'//set the max length value between fields
txtFieldParser.FieldWidths = New Integer() {15, 10}
'//we dont want to discard the space
txtFieldParser.TrimWhiteSpace = False
Do Until txtFieldParser.EndOfData
'//array of string to read the field value
Dim fields() As String = txtFieldParser.ReadFields
Console.WriteLine(String.Format("{0}{1}", fields(0), _
fields(1)))
Loop
End Using
End Sub
End Class