472,958 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 22980
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.