Error reading XML

davidh

Active member
Joined
Jul 8, 2003
Messages
31
Location
UK
Im new to using XML, so Ive been trying to work out some demos from help. Im trying to use an XMLReader to read an XML file but I keep getting an exception. In VS2003 is get an exception that says
"There is an invalid character in the given encoding. Line 1, position 3." and in VS2002 the same code and the same XML file gives the exception "Invalid byte was found at byte index 2."

Im using the following code

Imports System.Xml
Imports System.IO

Module Module1

Sub Main()
Dim reader As XmlTextReader = Nothing
Dim strFilePath As String = Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase _
& "\simple.xml")

Try
Load the reader with the data file and ignore all white space nodes.
reader = New XmlTextReader(strFilePath)
reader.WhitespaceHandling = WhitespaceHandling.None

Parse the file and display each of the nodes.
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
Console.Write("<{0}>", reader.Name)
Case XmlNodeType.Text
Console.Write(reader.Value)
Case XmlNodeType.CDATA
Console.Write("<![CDATA[{0}]]>", reader.Value)
Case XmlNodeType.ProcessingInstruction
Console.Write("<?{0} {1}?>", reader.Name, reader.Value)
Case XmlNodeType.Comment
Console.Write("<!--{0}-->", reader.Value)
Case XmlNodeType.XmlDeclaration
Console.Write("<?xml version=1.0?>")
Case XmlNodeType.Document
Case XmlNodeType.DocumentType
Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value)
Case XmlNodeType.EntityReference
Console.Write(reader.Name)
Case XmlNodeType.EndElement
Console.Write("</{0}>", reader.Name)
End Select
End While
Catch e As XmlException
Console.WriteLine(e.ToString)

Catch e As Exception
Console.WriteLine(e.ToString)
Finally
If Not (reader Is Nothing) Then
reader.Close()
End If
End Try

Console.ReadLine()
End Sub

End Module


and my XML file is

<?xml version="1.0"?>
<!-- This simple data represents food items in a grocery store -->
<GroceryStore>
<StoreName>Brians Groceries</StoreName>
<Departments>
<Department Name="Breads">
<Item ID="B1">
<Name>Wonder Bread</Name>
<Price>1.29</Price>
<New />
</Item>
<Item ID="B2" Type="Muffin">
<Name>Blueberry Muffin</Name>
<Price>3.99</Price>
<New />
</Item>
</Department>
<Department Name="Fruits">
<Item ID="F1">
<Name>Apple</Name>
<Price>0.99</Price>
</Item>
</Department>
</Departments>
</GroceryStore>


Am i missing something fundamental? I didnt write this code myself, I copied from the help that ships with VS2003 and the XML file came from an example I downloaded. Any help would be very much appreciated. Eventually I want to be reading an XML file on a compact framework app so if anyone has any pointers for that too I would be very grateful!

Thanks in advance
 
Hi, just have encountered the same problem.

It seems to come from the comment : if you erase the first 2 comment lines of your xml file this should work :
<?xml version="1.0"?>
<!-- This simple data represents food items in a grocery store -->
Maybe MS should update this, I am now searching how to get xml comment read. If anyine knows...

Bye
 
From what I know so far I can tell you that you sould leave the fisrt line in as it is a declaration. It is optional but tells the xml parser what version the xml document conforms to.

To access a parts of the xml file use XMLElement, XMLComment, XMLAttribute etc from the NodeType enum.
 
Back
Top