School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,975 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,862 people online right now. Registration is fast and FREE... Join Now!




TextFieldParser

 
Reply to this topicStart new topic

> TextFieldParser, How to use it.

AdamSpeight2008
Group Icon



post 28 Mar, 2009 - 02:42 PM
Post #1


Text Field Parser

The TextFieldParser helps you parses files that have predefine fields.
It can handle two types
  • Fixed Length Fields - Where each field is a different section of on a single string.
  • Delimited Fields - Where the fields are separated by a token (usually a comma).
Creating the two test files
Create a new folder in your Solution Explorer called Files.
In that folder create the following text files.
The first is TestFile01.txt, this is the fixed-length one.
CODE

' This line is comment
' Field Structure
'  0 - 3  Id
'  4 - 23 Name
' 24 - 31 Date Of Birth
'         1         2         3
'1234567890123456789012345678901
4567John Doe            02181999
8901Jane Smith Doe      03041972

The Second is TestFile02.txt, this the Separated Fields File.
CODE

' This line is comment
' Field Structure
' Id, Name , Date of Birth
' Note: I treat them as strings
"4567","John Doe","02/18/1999"
"8901","Jane Smith Doe","03/04/1972"

Set the Copy to Output Directory property of both files to Copy Always. This is to make sure we have to file in project so we can load them later.

Add new class to the Project call Person, give it following code.
CODE

Public Class person
Private m_ID As String
Public Property ID() As String
  Get
   Return m_ID
  End Get
  Set(ByVal value As String)
   m_ID = value
  End Set
End Property
Private m_Name As String
Public Property Name() As String
  Get
   Return m_Name
  End Get
  Set(ByVal value As String)
   m_Name = value
  End Set
End Property
Private m_DOB As String
Public Property DOB() As String
  Get
   Return m_DOB
  End Get
  Set(ByVal value As String)
   m_DOB = value
  End Set
End Property
End Class


The UI
This will be a very simple UI
Add a ListView object to the form set its View to Details
Add the the three columns to it, with column texts of (ID, Name, DoB)
Paste a copy of this listview on the form.
Now on to the Form Code.
CODE

Public Class Form1
Dim A_List As New List(Of person)
Dim B_List As New List(Of person)
End Class


In the Form1's load event
CODE

From_FixedLengthFile()
  AddPersonsToListview(A_List, Me.ListView1)
  From_DelimitedFile()
  AddPersonsToListview(B_List, Me.ListView2)

Below this add the follow helper routines.
CODE

Private Sub AddPersonsToListview(ByRef pl As List(Of person), ByRef lv As ListView)
  lv.Items.Clear()
  Dim clv As ListViewItem
  For Each p As person In pl
   clv = lv.Items.Add(p.ID)
   clv.SubItems.Add(p.Name)
   clv.SubItems.Add(p.DOB)
  Next
End Sub
Public Function ToDate(ByVal d As String) As Date
  Dim rm As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(d, "([0-9]{2})([0-9]{2})([0-9]{4})")
  If rm.Success = False Then Return Nothing
  Dim dr As New Date(CInt(rm.Groups(3).Value), CInt(rm.Groups(1).Value), CInt(rm.Groups(2).Value))
  Return dr
End Function


Finally the code to read the files.


A Fixed-Length Fields File

CODE

Private Sub From_FixedLengthFile()
  ' Define a new TextFieldParser, passing it our file
  Dim tfp As New FileIO.TextFieldParser(My.Application.Info.DirectoryPath & "\Files\TestFile01.txt")
  ' Tell the TextFieldParse to expect a FixedWidth file (By Default it Delimited)
  tfp.TextFieldType = FileIO.FieldType.FixedWidth
  ' Define an array containing the length of each field, in the order they appear
  Dim FieldLengths() As Integer = {4, 20, 8}
  ' Define an array containing the token to indicate what follows next (to the line end) is a comment
  Dim Comments() As String = {"'"}
  ' Tell our TextFieldParser what the parameters are.
  tfp.FieldWidths = FieldLengths
  tfp.CommentTokens = Comments
' Empty the collection of people
  A_List.Clear()
  Dim p As person
  ' Read the file in Try-Catch Block in case of file problems
  Try
   ' Have we read to the end of the file?
   While tfp.EndOfData = False
    ' Get the fields of the current line, it automatically moves to the next position.
    Dim FoundField() As String = tfp.ReadFields
    ' Does it contain the expected number of fields?
    If FoundField.Count <> 3 Then
     ' Nope, Display a message.
     MessageBox.Show(String.Format("Incorrect Number field on line {0}", tfp.LineNumber), "", MessageBoxButtons.OK)
    Else
     ' Yes, Create a new person object and fill in the details
     p = New person
     p.ID = FoundField(0)
     p.Name = FoundField(1)
     p.DOB = ToDate(FoundField(2))
     ' Add the person to our collection of people.
     A_List.Add(p)
    End If
   End While
  Catch ex As Exception
   Throw ex
  End Try
  ' Remember to close the parser.
  tfp.Close()
End Sub


A Delimited Fields File
CODE

Private Sub From_FixedLengthFile()
  ' Define a new TextFieldParser, passing it our file
  Dim tfp As New FileIO.TextFieldParser(My.Application.Info.DirectoryPath & "\Files\TestFile01.txt")
  ' Tell the TextFieldParse to expect a FixedWidth file (By Default it Delimited)
  tfp.TextFieldType = FileIO.FieldType.FixedWidth
  ' Define an array containing the length of each field, in the order they appear
  Dim FieldLengths() As Integer = {4, 20, 8}
  ' Define an array containing the token to indicate what follows next (to the line end) is a comment
  Dim Comments() As String = {"'"}
  ' Tell our TextFieldParser what the parameters are.
  tfp.FieldWidths = FieldLengths
  tfp.CommentTokens = Comments
  ' Empty the collection of people
  A_List.Clear()
  Dim p As person
  ' Read the file in Try-Catch Block in case of file problems
  Try
   ' Have we read to the end of the file?
   While tfp.EndOfData = False
    ' Get the fields of the current line, it automatically moves to the next position.
    Dim FoundField() As String = tfp.ReadFields
    ' Does it contain the expected number of fields?
    If FoundField.Count <> 3 Then
     ' Nope, Display a message.
     MessageBox.Show(String.Format("Incorrect Number field on line {0}", tfp.LineNumber), "", MessageBoxButtons.OK)
    Else
     ' Yes, Create a new person object and fill in the details
     p = New person
     p.ID = FoundField(0)
     p.Name = FoundField(1)
     p.DOB = ToDate(FoundField(2))
     ' Add the person to our collection of people.
     A_List.Add(p)
    End If
   End While
  Catch ex As Exception
   Throw ex
  End Try
  ' Remember to close the parser.
  tfp.Close()
End Sub


You'll notice the code is very similar to the Fixed-Length one.

That the basic of the TextFieldParser.

For the Adventurous
See what is possible to do with TextFieldParse and a little ingenuity.
Simple Scripting Language.
Simple Scripting Language Cont.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 05:09AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month