473,804 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy XML file -- three extra bytes???

Hi,

I am copying an xml file like so.

Dim xmlDoc As New XmlDocument
xmlDoc.Load("C: \Program Files\Templates \message.msg")
Console.WriteLi ne("Tmaplate loaded")
xmlDoc.Save("C: \Program Files\Templates \copy.xml")
Console.WriteLi ne("message saved")

Now the xml file copies and is capable of being end with IE, however the xml
file that is prodced is not able to be copied using the method above.

The reason is the produced xml file has three additional bytes at the start
of it (ie before the "<xml" part)

my question is.

does anybody know why this is and how to get rid of the three additional
bytes at the start of the file.

many thanks in advance.

martin.


Nov 18 '05 #1
4 2250
martin wrote:
Hi,

I am copying an xml file like so.

Dim xmlDoc As New XmlDocument
xmlDoc.Load("C: \Program Files\Templates \message.msg")
Console.WriteLi ne("Tmaplate loaded")
xmlDoc.Save("C: \Program Files\Templates \copy.xml")
Console.WriteLi ne("message saved")

Now the xml file copies and is capable of being end with IE, however the xml
file that is prodced is not able to be copied using the method above.

The reason is the produced xml file has three additional bytes at the start
of it (ie before the "<xml" part)

my question is.

does anybody know why this is and how to get rid of the three additional
bytes at the start of the file.


The file is being save in a Unicode encoding. The 3 additional byes are
a Unicode BOM (Byte Order Mark).

you can probably solve the problem by either specifying that the file is
encoded with Unicode in the <?xml ...> declaration tag, or by saving the
file in ASCII:

dim stream as StreamWriter
try
stream = New StreamWriter( "C:\Program Files\Templates \copy.xml",
false, System.Text.Enc oding.Default)

xmlDoc.Save( stream)
Console.WriteLi ne("message saved")
catch
Console.WriteLi ne( "Error saving file")
finally
if (Not stream Is nothing)
stream.Close()
end if
end try
--
mikeb
Nov 18 '05 #2
mikeb wrote:
martin wrote:
Hi,

I am copying an xml file like so.

Dim xmlDoc As New XmlDocument
xmlDoc.Load("C: \Program Files\Templates \message.msg")
Console.WriteLi ne("Tmaplate loaded")
xmlDoc.Save("C: \Program Files\Templates \copy.xml")
Console.WriteLi ne("message saved")

Now the xml file copies and is capable of being end with IE, however
the xml
file that is prodced is not able to be copied using the method above.

The reason is the produced xml file has three additional bytes at the
start
of it (ie before the "<xml" part)

my question is.

does anybody know why this is and how to get rid of the three additional
bytes at the start of the file.


The file is being save in a Unicode encoding. The 3 additional byes are
a Unicode BOM (Byte Order Mark).

you can probably solve the problem by either specifying that the file is
encoded with Unicode in the <?xml ...> declaration tag, or by saving the
file in ASCII:

dim stream as StreamWriter
try
stream = New StreamWriter( "C:\Program Files\Templates \copy.xml",
false, System.Text.Enc oding.Default)

xmlDoc.Save( stream)
Console.WriteLi ne("message saved")
catch
Console.WriteLi ne( "Error saving file")
finally
if (Not stream Is nothing)
stream.Close()
end if
end try


Clarification: the Unicode encoding that you're seeing is probably UTF-8.

In any case, I played around a little bit more with your sample code,
and I had to manually change the encoding specified in the input file to
be incorrect to get xmlDoc.Load() to throw an exception. In other words,
xmlDoc.Load() does not seem to mind the BOM header, unless the encoding
attribute in the <?xml ...?> tag is lying.

Can you post a very, very small XML file that causes the problem you're
seeing?
--
mikeb
Nov 18 '05 #3
You are correct,
The problem now becomes now to create an xml file with the line

<?xml version="1.0" encoding="UTF-8" standalone="yes " ?>

(with the UTF encoding set to 8)

using xmldocument.loa d

or do I just have to revert to your ascii method??

many thanks for the help, I have included samples below that demonstarte my
problem.
The xml file is generted in code rather than include files to this message.
=============== =
Try

Dim doc As New XmlDocument

doc.LoadXml("<? xml version=""1.0"" encoding=""UTF-8"" standalone=""ye s""?>"
& _

"<Message version=""1.1"" id="""">" & _

"<Attribute s>" & _

"<Priority> </Priority>" & _

"<DeleteAttache s></DeleteAttaches> " & _

"</Attributes>" & _

"</Message>")

doc.Save("C:\Pr ogram Files\Templates \ThreeByteError .xml")

Console.WriteLi ne("Saved the dodgy xml file")

doc.LoadXml("<? xml version=""1.0"" standalone=""ye s""?>" & _

"<Message version=""1.1"" id="""">" & _

"<Attribute s>" & _

"<Priority> </Priority>" & _

"<DeleteAttache s></DeleteAttaches> " & _

"</Attributes>" & _

"</Message>")

doc.Save("C:\Pr ogram Files\Templates \NoThreeByteErr or.xml")

Console.WriteLi ne("Saved the fine xml file")

Console.WriteLi ne("Press a key to close")

Console.ReadLin e()

Catch ex As Exception

Console.WriteLi ne("***ERROR*** ")

Console.WriteLi ne(ex.Message)

End Try

Console.WriteLi ne("Press a key to close")

Console.ReadLin e()

End Sub

=============== =

Now run the follwoing at he command line to see the problem

type "C:\Program Files\Templates \ThreeByteError .xml"

type "C:\Program Files\Templates \NoThreeByteErr or.xml"

fc "C:\Program Files\Templates \NoThreeByteErr or.xml" "C:\Program
Files\Templates \ThreeByteError .xml"
cheers

martin.
Nov 18 '05 #4
martin wrote:
You are correct,
The problem now becomes now to create an xml file with the line

<?xml version="1.0" encoding="UTF-8" standalone="yes " ?>

(with the UTF encoding set to 8)
Well, the documentation for StreamWriter indicates that a BOM will be
written unless the encoding used is Encoding.Defaul t.

However, at least for XmlDocument.Loa d(), the BOM poses no problem on my
machine - it loads just fine.

If there's some other software that you need to load the XML document
into that does not handle the BOM, I suppose you have a few options:

- write the file using Encoding.Defaul t.
- post-process the output file to remove the BOM
- upgrade the software that doesn't like the BOM to handle it properly

I'm sure there are others, too.

using xmldocument.loa d

or do I just have to revert to your ascii method??

many thanks for the help, I have included samples below that demonstarte my
problem.
The xml file is generted in code rather than include files to this message.
=============== =
Try

Dim doc As New XmlDocument

doc.LoadXml("<? xml version=""1.0"" encoding=""UTF-8"" standalone=""ye s""?>"
& _

"<Message version=""1.1"" id="""">" & _

"<Attribute s>" & _

"<Priority> </Priority>" & _

"<DeleteAttache s></DeleteAttaches> " & _

"</Attributes>" & _

"</Message>")

doc.Save("C:\Pr ogram Files\Templates \ThreeByteError .xml")

Console.WriteLi ne("Saved the dodgy xml file")

doc.LoadXml("<? xml version=""1.0"" standalone=""ye s""?>" & _

"<Message version=""1.1"" id="""">" & _

"<Attribute s>" & _

"<Priority> </Priority>" & _

"<DeleteAttache s></DeleteAttaches> " & _

"</Attributes>" & _

"</Message>")

doc.Save("C:\Pr ogram Files\Templates \NoThreeByteErr or.xml")

Console.WriteLi ne("Saved the fine xml file")

Console.WriteLi ne("Press a key to close")

Console.ReadLin e()

Catch ex As Exception

Console.WriteLi ne("***ERROR*** ")

Console.WriteLi ne(ex.Message)

End Try

Console.WriteLi ne("Press a key to close")

Console.ReadLin e()

End Sub

=============== =

Now run the follwoing at he command line to see the problem

type "C:\Program Files\Templates \ThreeByteError .xml"

type "C:\Program Files\Templates \NoThreeByteErr or.xml"

fc "C:\Program Files\Templates \NoThreeByteErr or.xml" "C:\Program
Files\Templates \ThreeByteError .xml"
cheers

martin.

--
mikeb
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3319
by: RonHiler | last post by:
My copy constructor is crashing my program, and I can't figure out why. I'll try to make the code listing as short as I can. Here are the two headers: class BreakthroughClass { public: BreakthroughClass(); virtual ~BreakthroughClass(); BreakthroughClass(const BreakthroughClass &source);
5
4355
by: zambak | last post by:
Hi I have assignment for some wierd compression alghoritam that will read in from a file convert characters to 5 bit codes and then write out compressed version of the original file. For example if input file contains string "AMIR" and the codes are A=0, M=12,I=8,R=17 i am supposed to write out 3 byte file The problem? How do I figure out shifting because I can only write out bytes
8
12467
by: Alex | last post by:
Hi all, can someone please show me how to get the size of a file with ANSI C? both in text mode and binary mode. thanks in advance.
26
3007
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier interupted before it could write the variables to the file, the file is gonna be empty, and then it's gonna load a load of crap into variables, which i want to avoid. That file is always 36 bytes big (it contains 4 double-precision floats and one...
4
2514
by: Phillip Ian | last post by:
Version: VS 2005 I took the sample code from help about encrypting and decrypting strings, and changed it to work directly with byte arrays and get the key and IV values from functions I've written to privide them. No other changes were made... Friend Function EncryptBytes(ByVal Data() As Byte, ByVal APassphrase As String) As Byte() Try
9
8244
by: Alan T | last post by:
Is it possible to copy a file from one location to another? eg. from C:\Temp\Document\TestDoc.doc to C:\Deploy\Document\TestDoc.doc
4
5710
by: pradqdo | last post by:
Hi folks, I have a very strange problem when I try to port my client/server program to cygwin. It is a simple shell program where the server executes client's commands + it can send and receive files (something like ftp server/client). I implemented all the commands which the server executes from scratch meaning I don't use fork and exec. It was very educational but my problem is that when I try to "get <filename>" from server to the...
6
32923
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line). Is there some way to burst read the whole file and later "extract" each line? Thanks in advance, Thomas Kowalski
3
3082
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt functions. While my functions read and write files the encryption/decryption is not working properly. My test file has an original length of 66,048 bytes. My encrypted file ends up with 66,056 bytes … 8 bytes more than my original. When I...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.