Read XML from a File Using VB.net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
***************************
 
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
---------------------
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
***************************
 
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?
 
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
 
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
-----------------------
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
 
Back
Top