469,268 Members | 920 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Reading XML file with VB.NET

Hi all,

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"?>

I believe the encoding tag is restricting my efforts. This same file was
generated by using the XSL.Transform class but I did not instruct it to give
it this encoding. What do I have to do to get this working?

Thanks,
Jesse

Nov 12 '05 #1
2 22744
"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
Nov 12 '05 #2
Thanks for the reply Derek but i still fall down trying to load the document.
I have the following code now as you suggested.

textDoc = New StreamWriter(MapPath(".") & "\logfile.txt")
textDoc.WriteLine("1")

Dim yourFileStream As FileStream
yourFileStream = New FileStream(MapPath(".") & "\basic2.xml",
FileMode.Open)

textDoc.WriteLine("2")

Dim sReader As System.IO.StreamReader
sReader = New System.IO.StreamReader(yourFileStream,
System.Text.Encoding.Unicode, False)

textDoc.WriteLine("3")

Dim xmlSource As System.Xml.XmlTextReader
xmlSource = New System.Xml.XmlTextReader(sReader)

textDoc.WriteLine("4")

Dim xmlDoc As System.Xml.XmlDocument
xmlDoc = New System.Xml.XmlDocument
xmlDoc.Load(xmlSource)

textDoc.WriteLine("5")

yourFileStream.Close()

The output to the logfile is:
1
2
3
4
The data at the root level is invalid. Line 1, position 1.

Any suggestions?

Jesse

"Derek Harmon" wrote:
"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

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Xah Lee | last post: by
1 post views Thread by fabrice | last post: by
19 posts views Thread by Lionel B | last post: by
4 posts views Thread by Oliver Knoll | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
7 posts views Thread by John Dann | last post: by
6 posts views Thread by efrenba | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.