|
-
May 12th, 2009, 11:43 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Read plain text delimited by fieldnames
Hi,
for example I have this plain text:
ID=100000CUSTOMER=JohnAMOUNT=2300020INVOICES=123CALLS=12ID=100001CUSTOMER=MaryAMOUNT=589INVOICES=2CA LLS=0ID=1002122CUSTOMER=DanielAMOUNT=8900000INVOICES=5CALLS=1
etc etc
Where "ID", "CUSTOMER", "AMOUNT", "INVOICES", "CALLS" are fields and I need get its values.
is there any way, most automatic possible, to recover those values? If they were delimited by fixed delimiter, I could do it with split......
thanks in advance
-
May 12th, 2009, 12:28 PM
#2
Re: Read plain text delimited by fieldnames
You can try something like this:
Code:
'Create a datatable to hold the records
Dim myTable As New DataTable()
With myTable.Columns
.Add("ID", GetType(Integer))
.Add("CUSTOMER", GetType(String))
.Add("AMOUNT", GetType(Decimal))
.Add("INVOICES", GetType(Integer))
.Add("CALLS", GetType(Integer))
End With
'Start parsing the text into records and populate the datatable
Dim txt As String = "ID=100000CUSTOMER=JohnAMOUNT=2300020INVOICES=123CALLS=12ID=100001CUSTOMER=MaryAMOUNT=589INVOICES=2CA LLS=0ID=1002122CUSTOMER=DanielAMOUNT=8900000INVOICES=5CALLS=1"
Dim delimiters() As String = {"ID="}
Dim records() As String = txt.Split(delimiters, System.StringSplitOptions.RemoveEmptyEntries)
For Each record As String In records
Dim fields() As String = record.Split("=".ToCharArray)
If fields.Length = 5 Then
Dim row As DataRow = myTable.NewRow()
row("ID") = CInt(fields(0).Replace("CUSTOMER", ""))
row("CUSTOMER") = fields(1).Replace("AMOUNT", "")
row("AMOUNT") = CDec(fields(2).Replace("INVOICES", ""))
row("INVOICES") = CInt(fields(3).Replace("CALLS", ""))
row("CALLS") = CInt(fields(4))
myTable.Rows.Add(row)
End If
Next
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
May 12th, 2009, 02:14 PM
#3
Thread Starter
Hyperactive Member
Re: Read plain text delimited by fieldnames
stanav, thank you very much. Perfect example :-)
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
|