473,503 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read XML from a File Using VB.net

Article ID: 301225
I can't seem to get this code to work.
I modified it alittle so I could try and figure out the exception.
I keep getting an error on the " Do While (reader.Read()) " statement.
I'm using the books.xml file MS supplies for the sample code.
Am I that far off in left field??? or is there help available???
******************************
Option Strict On
Option Explicit On
Imports System.Xml
Imports Microsoft.VisualBasic
Imports System
Imports System.IO

Module Module1

Sub Main()
Dim reader As XmlTextReader = Nothing
Console.WriteLine("This is before the loop.")

Try
reader = New XmlTextReader("books.xml")
reader.WhitespaceHandling = WhitespaceHandling.None

reader.MoveToContent()

Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'display beginning of element.
Console.WriteLine("<" + reader.Name)
If reader.HasAttributes Then 'if attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value....
Console.Write(" {0}='{1}'", reader.Name,
reader.Value)
End While
End If
Console.WriteLine(">")
Case XmlNodeType.Text 'Display the text in each element.
Console.WriteLine(reader.Value)
Case XmlNodeType.EndElement 'Display end of element.
Console.WriteLine("</" + reader.Name)
Console.WriteLine(">")
End Select
Loop
Console.ReadLine() 'Pause
Catch e As Exception
Console.WriteLine("exception: {0}", e.ToString())
Console.WriteLine("**********************")
System.IO.StringWriter.Null.WriteLine("exception: {0}", e.ToString())
Finally
Console.WriteLine("End routine")
Console.ReadLine() 'Pause
End Try
End Sub

End Module
***************************
--
Thanks Very Much
BobC.
Nov 21 '05 #1
5 1772
Hi,

You have the end while and end if in the wrong order.

If reader.HasAttributes Then 'if attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value....
Console.Write(" {0}='{1}'", reader.Name,
reader.Value)
End If
End While
Ken
---------------------
"BobC" <Bo**@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
Article ID: 301225
I can't seem to get this code to work.
I modified it alittle so I could try and figure out the exception.
I keep getting an error on the " Do While (reader.Read()) " statement.
I'm using the books.xml file MS supplies for the sample code.
Am I that far off in left field??? or is there help available???
******************************
Option Strict On
Option Explicit On
Imports System.Xml
Imports Microsoft.VisualBasic
Imports System
Imports System.IO

Module Module1

Sub Main()
Dim reader As XmlTextReader = Nothing
Console.WriteLine("This is before the loop.")

Try
reader = New XmlTextReader("books.xml")
reader.WhitespaceHandling = WhitespaceHandling.None

reader.MoveToContent()

Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element 'display beginning of element.
Console.WriteLine("<" + reader.Name)
If reader.HasAttributes Then 'if attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value....
Console.Write(" {0}='{1}'", reader.Name,
reader.Value)
End While
End If
Console.WriteLine(">")
Case XmlNodeType.Text 'Display the text in each element.
Console.WriteLine(reader.Value)
Case XmlNodeType.EndElement 'Display end of element.
Console.WriteLine("</" + reader.Name)
Console.WriteLine(">")
End Select
Loop
Console.ReadLine() 'Pause
Catch e As Exception
Console.WriteLine("exception: {0}", e.ToString())
Console.WriteLine("**********************")
System.IO.StringWriter.Null.WriteLine("exception: {0}",
e.ToString())
Finally
Console.WriteLine("End routine")
Console.ReadLine() 'Pause
End Try
End Sub

End Module
***************************
--
Thanks Very Much
BobC.
Nov 21 '05 #2
> I modified it alittle so I could try and figure out the exception.
I keep getting an error on the " Do While (reader.Read()) "

statement.

Are we supposed to use ESP to figure out what error you are getting?

Nov 21 '05 #3
I don't see what you mean, Ken. The End If should be *outside* the
while loop as the code posted shows.

Do While (reader.Read())
'snip

If reader.HasAttributes Then 'if attributes exist
While reader.MoveToNextAttribute()
'snip
End While
End If
Loop

Nov 21 '05 #4
Hi,

Sorry miss read code I usually use

While MyCondition
End while

So i thought he was trying to loop back in the middle of an if then
statement. Anyway if he is trying to exit a do while loop use exit do
not end while.

Ken
-----------------------
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I don't see what you mean, Ken. The End If should be *outside* the
while loop as the code posted shows.

Do While (reader.Read())
'snip

If reader.HasAttributes Then 'if attributes exist
While reader.MoveToNextAttribute()
'snip
End While
End If
Loop
Nov 21 '05 #5
Chris,
Are we supposed to use ESP to figure out what error you are getting?


And every day we learn in this newsgroup.

extrasensory perception

:-)

Cor
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6006
by: Anne-Marte | last post by:
Hi I simply don't understand how to read a simple file using std::istream. How do I open a file for reading with istream?? Anne-Marte
14
9070
by: Mark Broadbent | last post by:
Does anybody know what is (factual please -not just guess) the quickest method to read data from a file? I am not interested in the format of the data (i.e. blocks, bytes, string etc) just that the...
4
2711
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
5
7515
by: JenHu | last post by:
Hi experts, I wrote a function which retrieves a file in the folder, the file path is : Dim sr As New StreamReader(strFilepath & ReturnFileName) What if I have more than 1 file_name in...
8
7242
by: james | last post by:
I am trying to use Filestream to read a file ( .DAT) that contains values in HEX that I want to convert to text. I know the different offset addresses for each portion of the data I am trying to...
8
23884
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
5070
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
3
4193
by: Yaniv | last post by:
Hi I'm new in VB.NET. I wrote an application which opens a text file and read it all lines untill the EOF this file is open for read only and for sharing asllowed. every 5 seconds another...
3
1793
by: Ray | last post by:
Hello World, I made a Windowsform that reads data from a CSV file. It works fine, but when I have read the data of a record I have to re-Debug the form to read another record. So when I put a...
6
32846
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.