"Jessard" <Je*****@discussions.microsoft.com> wrote in message news:45**********************************@microsof t.com...
I am having trouble reading an XML document in and looping through it's
nodes in VB.NET. The error is specific, it is:
"There is no Unicode byte order mark. Cannot switch to Unicode."
The file that I am trying to read begins with:
<?xml version="1.0" encoding="UTF-16"?>
How are you opening this File? If you're using a FileStream, you
need to wrap the FileStream in a StreamReader and specify the
encoding and not to use the Byte-Order Mark (BOM), like this:
Dim reader As System.IO.StreamReader
reader = New System.IO.StreamReader( _
yourFileStream, _
System.Text.Encoding.Unicode, _
False )
Then, to get this into an XmlDocument, for example,
Dim xmlSource As System.Xml.XmlTextWriter
xmlSource = New System.Xml.XmlTextWriter( reader)
Dim xmlDoc As System.Xml.XmlDocument
xmlDoc = New System.Xml.XmlDocument( )
xmlDoc.Load( xmlSource)
yourFileStream.Close( )
Note that it's always best to open the FileStream (or XmlTextWriter with
a filespec) separately and then Close( ) it after the XmlDocument has
loaded to avoid an exclusive lock being held on the file til it gets disposed.
Derek Harmon