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 :
1: Imports System.IO
2: Imports Microsoft.VisualBasic.FileIO
3:
4: Public Class frmTextFieldParser
5:
6: Private Sub frmTextFieldParser_Load(ByVal sender As System.Object, _
7: ByVal e As System.EventArgs) Handles MyBase.Load
8: Dim sw As StreamWriter
9:
10: '================creating comma delimited text file===========
11: sw = New StreamWriter("C:\books1.txt", False)
12: sw.WriteLine(String.Format("{0},{1}", "VB 2005", "Que"))
13: sw.WriteLine(String.Format("{0},{1}", "C# 2005", "Apress"))
14: sw.WriteLine(String.Format("{0},{1}", "C++ 2005", _
15: "MS Press"))
16: sw.Flush()
17: sw.Close()
18:
19:
20: '=================creating fixed-length char text file========
21: Dim listOfTitle As New List(Of String)
22: listOfTitle.AddRange(New String() {"VB 2005", "C# 2005", _
23: "C++ 2005"})
24:
25: '//15 fixed-length char title
26: Const constMaxLengthTitle As Integer = 15
27:
28: '//the difference length between title and max length
29: '//for separating value between fields
30: Dim intTitleDiffLength(2) As Integer
31: Dim intCounter As Integer = 0
32: For Each aTitle As String In listOfTitle
33: Dim intTitleLength As Integer = aTitle.Length
34: intTitleDiffLength(intCounter) = constMaxLengthTitle - _
35: intTitleLength
36: intCounter += 1
37: Next
38:
39: Dim listOfPublishers As New List(Of String)
40: listOfPublishers.AddRange(New String() {"Que", "Apress", _
41: "MS Press"})
42:
43: '//10 fixed-length char publisher
44: Const constPublisherLength As Integer = 10
45:
46: '//the difference length between publisher and max length
47: '//for separating value between fields
48: Dim intPubDiffLength(2) As Integer
49: intCounter = 0
50: For Each aPublisher As String In listOfPublishers
51: Dim intPublisherLength As Integer = aPublisher.Length
52: intPubDiffLength(intCounter) = constPublisherLength - _
53: intPublisherLength
54: intCounter += 1
55: Next
56:
57: '//loop all the element of title and publisher
58: '//to write its value to the text file
59: sw = New StreamWriter("C:\books2.txt", False)
60: For intRec As Integer = 0 To listOfTitle.Count - 1
61: '//space between field is created automatically
62: '//based on the difference length between fields value
63: '//and allowed max length
64: sw.WriteLine(String.Format("{0}{1}", listOfTitle(intRec) & _
65: New String(" ", intTitleDiffLength(intRec)), _
66: listOfPublishers(intRec) & New String(" ", _
67: intPubDiffLength(intRec))))
68: Next
69: sw.Flush()
70: sw.Close()
71: End Sub
72:
73: Private Sub btnDelimited_Click(ByVal sender As System.Object, _
74: ByVal e As System.EventArgs) Handles btnDelimited.Click
75: Using txtFieldParser As New TextFieldParser("C:\books1.txt")
76: '//assign the TextFieldType property value
77: '//to the FieldType.Delimited enumeration value
78: txtFieldParser.TextFieldType = FieldType.Delimited
79:
80: '//obtaining the delimiter char
81: txtFieldParser.Delimiters = New String() {","}
82:
83: '//discard the space (if we wanted to)
84: txtFieldParser.TrimWhiteSpace = True
85:
86: Do Until txtFieldParser.EndOfData
87: '//array of string to read the field value
88: Dim fields() As String = txtFieldParser.ReadFields
89: Console.WriteLine(String.Format("{0} {1}", fields(0), _
90: fields(1)))
91: Loop
92: End Using
93: End Sub
94:
95: Private Sub btnFixedLength_Click(ByVal sender As System.Object, _
96: ByVal e As System.EventArgs) Handles btnFixedLength.Click
97: Using txtFieldParser As New TextFieldParser("C:\books2.txt")
98: '//assign the TextFieldType property value
99: '//to the FieldType.FixedWidth enumeration value
100: txtFieldParser.TextFieldType = FieldType.FixedWidth
101:
102: '//set the max length value between fields
103: txtFieldParser.FieldWidths = New Integer() {15, 10}
104:
105: '//we dont want to discard the space
106: txtFieldParser.TrimWhiteSpace = False
107:
108: Do Until txtFieldParser.EndOfData
109: '//array of string to read the field value
110: Dim fields() As String = txtFieldParser.ReadFields
111: Console.WriteLine(String.Format("{0}{1}", fields(0), _
112: fields(1)))
113: Loop
114: End Using
115: End Sub
116: End Class