|
-
Oct 9th, 2012, 11:26 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] TextFieldParser ReadFields call by name instead of position
Is it possible to use TextFieldParser and call .ReadFields and then use field name instead of field position?
For example, I have 40 columns in my .csv file.
field 0 = ID
field 1 = brand
field 2 = model
field 3 = price
field 4 = color
field 5 = something else.....
How the data would appear in the file:
ID,brand,model,price,color,somethingelse
1,Samsung,Galaxy S,200,black,xx
2,Samsung,Galaxy S II,300,black,xy
3,Samsung,Galaxy S III,500,black,xy
4,Samsung,Galaxy S III,500,white,xy
Right now I am calling the array like so CurrentRecord(4) which returns the color field value.
Can I call it like so, CurrentRecord("color") which would return the same value?
Code:
Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(sFullPath)
Dim CurrentRecord As String() ' this array will hold each line of data
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
afile.HasFieldsEnclosedInQuotes = True
' parse the actual file
Dim iRow As Integer = 1
Do While Not afile.EndOfData
Try
CurrentRecord = afile.ReadFields
If iRow = 1 Then
'headers
Else
If CurrentRecord(39).Trim.Length > 0 Then
Dim sRetVal As String = InsertUpdateDB(CurrentRecord(0), CurrentRecord(19), CurrentRecord(20), _
CurrentRecord(21), CurrentRecord(21), CurrentRecord(22), _
CurrentRecord(23), CurrentRecord(24), CurrentRecord(25), _
CurrentRecord(26), CurrentRecord(27), CurrentRecord(28), _
CurrentRecord(29), CurrentRecord(30), CurrentRecord(31), _
CurrentRecord(32), CurrentRecord(33), CurrentRecord(34), _
CurrentRecord(35), CurrentRecord(36), CurrentRecord(37), _
CurrentRecord(38), CurrentRecord(39))
Else
Dim sRetVal As String = InsertUpdateDB(CurrentRecord(0), CurrentRecord(19), CurrentRecord(20), _
CurrentRecord(21), CurrentRecord(21), CurrentRecord(22), _
CurrentRecord(23), CurrentRecord(24), CurrentRecord(25), _
CurrentRecord(26), CurrentRecord(27), CurrentRecord(28), _
CurrentRecord(29), CurrentRecord(30), CurrentRecord(31), _
CurrentRecord(32), CurrentRecord(33), CurrentRecord(34), _
CurrentRecord(35), CurrentRecord(36), CurrentRecord(37), _
CurrentRecord(38), 0)
End If
End If
Catch ex As FileIO.MalformedLineException
ltlOutput.Text &= "<br />" & ex.ToString
Stop
End Try
iRow += 1
Loop
-
Oct 10th, 2012, 01:51 AM
#2
Re: TextFieldParser ReadFields call by name instead of position
The answer is No. As the .ReadFields returns the values in an array, you can read the values using the Index value.
Use LINQ
Code:
'Imports System.Linq
Dim lstLines = From line As String In System.IO.File.ReadAllLines("C:\test.txt") Skip 1 _
Let L = line.Split(",".ToCharArray()) _
Select New Mobile() With {.ID = L(0), .brand = L(1), .model = L(2), .price = L(3), .color = L(4), .somethingelse = L(5)}
For Each M As Mobile In lstLines
'Read the Lines by one one
MessageBox.Show(M.ID)
Next
Please mark you thread resolved using the Thread Tools as shown
-
Oct 10th, 2012, 08:28 AM
#3
Thread Starter
Fanatic Member
Re: TextFieldParser ReadFields call by name instead of position
danasegarane, thanks for the info. Very good stuff. I was hoping for a solution where I don't need to know the position of data so if say the source comes in without a few fields (which is ok) I can still read most of the data based on field/header names.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|