Groups | Blog | Home
all groups > vb.net data > october 2006 >

vb.net data : Reading from a text file



John Barleycorn
10/25/2006 3:47:12 PM
Hi

I'm a bit of (ie complete) a newbie when it comes to VB, but I need to do
some coding, and have come across my first hurdle - I hope someone can help
me!

Using VB.NET Express, I can open a text file and read the contents into a
TextBox.

What I want to do though is to ignore the lines BEFORE the following lines:

<type name="lstCountry" default="" inherits="combo" regex="" regexsample="">
<values>
and then to display all the following lines UNTIL the line is
</values>

But so far this is eluding me, and I suspect it is because of the complex
nature of the first line.

Can someone give me some pointers, please?

JB

John Barleycorn
10/25/2006 3:51:48 PM
Might help if I showed you what I had so far!:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
InputFile = New StreamReader("C:\TEMP\test\design.xml", True)

Do while Line not "<type name=\"lstCountry\" default=\"\" inherits=\"combo\"
regex=\"\" regexsample=\"\">"
Line = InputFile.ReadLine
TextBox1.Text = Line
Loop


'TextBox1.Text = InputFile.ReadToEnd()
InputFile.Close()
End Sub
End Class
[quoted text, click to view]

John Barleycorn
10/26/2006 7:25:46 PM
This is really getting to me now. I now have the following code, and I am
trying to load a text file line by line into a checked list box (simple?)

Any ideas?

Thanks

JB

Imports System
Imports System.IO

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("C:\TEMP\design.xml")
Dim line As String
' Read and display the lines from the file in ChkListBox1
' until the end of the file is reached.
Do
line = sr.ReadLine()

If line Is Not Nothing Then
CheckedListBox1.Items.Add(line)
End If


' TextBox1.Text = line
'Console.WriteLine(line)
Loop Until line Is Nothing
sr.Close()

End Sub
End Class






[quoted text, click to view]

Rick
10/27/2006 4:48:03 AM
John,

Since you seem to be reading from an xml file, why not use an XMLReader to
read it, then you can use its native methods - probably SelectSingleNode to
get the text you want.

I don't know the entire format of your xml, but something like this in
general should work (completely untested):

get a new xmlreader and load the xml

Dim node as XMLNode = xmlReader.SelectSingleNode("type")
if node isNot Nothing then
node.InnerXML 'this is the text value of the "type" node

....

This might get you started in the right direction.

Rick


[quoted text, click to view]

JerryWEC
10/27/2006 3:16:35 PM
John, your first attempt is close...

But, you need to set a break point on the Do While line of code and see what
your getting.
If the "<type name=" string is the first line in your XML file then the loop
will never execute the body of the loop...

[quoted text, click to view]

And, I'm not sure the loop is doing what you think. To do a Not Equal in VB
try <> (Assuming Line is a string object).

Most of the time you see Do While InputFile.Read() or Do While
InputFile.ReadLine(). Then once inside the body of your loop you can test
for each line of data rows to process or parse.

There are other option depending on what your are trying to do.

Method 1] This previous way is something you have to know how to do anyway
(Reading and parsing a file, any kind of file).

Method 2] Rick's suggestion using XML document and nodes. (I know XML but
have not played with the XML dom much).

Method 3] Create a DataSet object and load the XML file using built-in
methods from the DS to load the XML file directly into the DS. Then you can
cycle through the records in the datatable object to add them to another
DataSet or process them. Use a DataAdapter object to connected SQL Server
table. Once you update a DataTable object within a DS (DataSet) you can
update the data back to the database using the DA object. Check out the Web
for ADO code samples.

I think you have a great opportunity to learn three ways to read data from
an XML file using DotNet.

I to should do this.

JerryM

AddThis Social Bookmark Button