Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Go Back  Xtreme Visual Basic Talk > > > Retrieving data from text fileusing streamreader


Reply
 
Thread Tools Display Modes
  #1  
Old 02-15-2012, 05:42 PM
wilson208's Avatar
wilson208 wilson208 is offline
Newcomer
 
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
Default Retrieving data from text fileusing streamreader


Hi, I am making a simple application that will be able to store competition data into a text file that I have given a custom extension to on saving. Here is an example of the format that i have saved the data in.

Code:
[Comp_Name]Test Competition[\Comp_Name]
[Location]Silverwood Lurgan[\Location]
[Type]Teams[\Type]
[AgeGroup]Under17[\AgeGroup]
[Date]16/02/2012[\Date]
[Duration]3[\Duration]
[Entries]20[\Entries]
[LanesAvailable]5[\LanesAvailable]
When i open the file, i want to redisplay the data in separate text boxes. Later on in the project i want to be able to search within the file for headings for example;

if "[Heat1]" is present then get all text between [Heat1] and [\Heat1]

i am currently able to use the streamreader and open file dialog to retrieve the entire string of text fromthe however i am not sure how to go about getting the text between the headings I have used. Any help or general guidance would be greatly appreciated.
Many thanks, Wilson.
Reply With Quote
  #2  
Old 02-16-2012, 02:36 AM
DrPunk's Avatar
DrPunkRetrieving data from text fileusing streamreader DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

If you have a entire string then you are able to split that by carriage return line feed into an array of all the lines. You can then loop over the array and look at the content of each line, searching each line for whatever it is you're looking for.
Code:
Dim lines() as String ' Array to hold the lines
Dim line as string ' variable used to loop over lines

' The file has already been read into a string variable called fileContents
lines = fileContents.Split(vbcrlf)

' Can now loop over the lines of the file
For each line in lines
   if line.StartsWith("[Heat]") Then
       ' This looks like a heat line
   end if
next
I'd personally advise looking at using a Dataset instead. You can Read/Write your data very easily using ReadXML\WriteXML method of the Dataset, and once your data is inside a dataset it's much easier to work with.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 02-17-2012, 04:14 AM
wilson208's Avatar
wilson208 wilson208 is offline
Newcomer
 
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
Default

Thanks a lot, have started learning a bit about xml. One question, do I need to use an xml schema to write the data to an xml file? Thanks for your help.
Reply With Quote
  #4  
Old 02-17-2012, 06:21 AM
DrPunk's Avatar
DrPunkRetrieving data from text fileusing streamreader DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

By far the easiest way is to use Visual Studios dataset designer.

In the project menu, add new item, you select to add a dataset. You can then add the tables to your dataset as you need them and it'll set up all your objects for you.

You don't really need to know anything about XML. It just writes your dataset out as XML and lets you read the data back into the dataset to use. You never even have to look at the contents of the xml file.

As a simple example of how easy it is...

1. Add a dataset to your project (Project -> New Item -> Dataset, I've called the dataset MyDataset)
2. Add a datatable to the dataset (Right click the dataset designer and click add, then Datatable. I've then renamed the datatable to MyDataTable)
3. Add a column to the datatable (Right click the datatable and click add, then column. I've then renamed the column to MyColumn).

So, I've got a dataset called MyDataSet. That dataset contains a table called MyDatatable. That table contains one column called MyColumn. VB will have created loads of code to make using this quite simple.

Now, looking at using it. In the form for the project, add a textbox, a button and a datagridview. The button will add the textbox contents to the datatable (in the dataset). The datagridview will display the contents of the datatable.

In the form we want to have this Dataset we've created to use...
Code:
Public Class Form1

    ' Note how this is declared as the dataset that was created
    Dim ds As New MyDataSet

End Class
When the form loads it is going to attempt to read any data that has been saved. This assumes that there's a Temp folder in the root of C:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' Check if the file exists
    If IO.File.Exists("C:\Temp\data.xml") Then
        Try
            ' If the file does exist then attempt to read the file into our dataset object
            ds.ReadXml("C:\Temp\data.xml")
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error reading Xml")
        End Try
    End If

    ' Set up the datagridview to display the data
    ' Note how the Table is the table that we created in the dataset designer. It's all
    ' properly typed.
    DataGridView1.DataSource = ds.MyDataTable
End Sub
When the button is clicked it's going to add the textbox contents to the datatable
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Check that there's something in textbox1 and quit if not.
    If String.IsNullOrEmpty(TextBox1.Text.Trim) Then Exit Sub

    ' Declare a new row to add. Note how the type is MyDataTableRow. Again, it's properly types.
    Dim dr As MyDataSet.MyDataTableRow
    ' Create the new row from the dataset's table
    dr = ds.MyDataTable.NewRow
    ' Add the texttbox contents. Again, note how the row has the MyColumn property
    ' that was added to the datatable in the designer.
    dr.MyColumn = TextBox1.Text
    
    ' Add the row to the datatable. It will automatically appear in the datagridview
    ds.MyDataTable.Rows.Add(dr)
End Sub
And finally, when the form closes it will write the data ready for it to be read when it's next loaded.
Code:
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    Try
        ' Tell the dataset to write its data as XML
        ds.WriteXml("C:\Temp\data.xml")
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error writing Xml")
    End Try
End Sub
And that's it. It's all rather tidy.

You can loop over the contents of the data via...
Code:
Dim dr As MyDataSet.MyDataTableRow
For each dr in ds.MyDataTable.Rows
     debug.print dr.MyColumn
Next
You can query data out of the datatable like it's a database...
Code:
Dim drs() as MyDataSet.MyDataTableRow
Dim dr as MyDataSet.MyDataTableRow

drs = ds.MyDataTable.Select("MyColumn = 'Bob')
For each dr in drs
    debug.print(dr.MyColumn) ' Obviously they're all going to be Bob
Next
Yum.
__________________
There are no computers in heaven!
Reply With Quote
  #5  
Old 02-17-2012, 09:50 AM
wilson208's Avatar
wilson208 wilson208 is offline
Newcomer
 
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
Default

Hi, thanks so much for your help. I have made up my dataset using the dataset designer. However when I declare the dataset at the very start of the class as so:
Code:
Public Class NewComp1
    Dim ds As New CompetitionData
i get the following error:

Type 'CompetitionData' is not defined.

CompetitionData.xsd is the name of my dataset and I set the modifier to public.

Any ideas as to where I am going wrong?

Thanks, Wilson.
Reply With Quote
  #6  
Old 02-20-2012, 04:45 AM
DrPunk's Avatar
DrPunkRetrieving data from text fileusing streamreader DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

It might depend on when you called it competitiondata.

If you created it as Dataset1 and then renamed it CompetitionData then it may still be called Dataset1.

If you created it as CompetitionData then I'm not too sure what the problem is. Press F2 to load the object explorer. Try and find you application's name in the treeview of namespaces. Expand it and it'll show you the objects in your project. You should be able to see what it's calling your dataset there.
__________________
There are no computers in heaven!
Reply With Quote
Reply

Tags
retrieve data from text, string manipulation


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
 
Retrieving data from text fileusing streamreader
Retrieving data from text fileusing streamreader
 
-->