Click here to Skip to main content
15,116,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Afternoon everybody.

I'm trying to generate a text file for our Electronic Invoicing supplier to digitally firm and generate the XML and the PDF file for the company I work in.

I'm using the "FileOpen" command in VB NET 2017, just I used to do with VB 6.0 and VB Net 2010 a few years ago, and in fact, I'm creating the output text file correctly.

this is the code:

strFileName = strPath + "Facturas_Texto\" + IDFiscal + ".txt"
 If System.IO.File.Exists(strFileName) Then
 End If
 FileOpen(1, strFileName, OpenMode.Output)

I'm using the statement "Print" to write the required lines one by one.

The point now is our vendor wants the file enconding with UTF-8 and avoid BOM.

I have read this encoding characters and avoiding the BOM was a matter of building HTML and XML files, but I´ve never tried before to achieve this in a text file, specially generating it thru VB net 2017.

I saw I can edit the file thru the notebook and save it with this UTF-8 encoding, but I need to do it via the vB net code.

I'd really appreciate any help, or suggestion about how to do this or where to look for information about it.

Thank you very much.

What I have tried:

I am searching thru the Internet trying to find a way to do this.
Updated 5-Mar-18 21:15pm

1 solution

You have to use StreamWriter[^] class which supports encoding.

As to the UTF-8 without BOM... Please, read important note about Encoding.UTF8 Property (System.Text)[^]
To instantiate a UTF8 encoding that doesn't provide a BOM, call any overload of the UTF8Encoding constructor.

So, i'd suggest to read UTF8Encoding Constructor (Boolean, Boolean) (System.Text)[^] with parameters set to false and true.


Note: if you want to use data for further processing, you can write it into xml directly.
XML Serialization – Tips & Tricks[^]
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]
Miguel Altamirano Morales 6-Mar-18 10:23am
Hello again, Maciej; and thanks a lot for your advice. You are such a nice person.

I'm going to read all about the StreamWriter class as you suggest. I hope it'll be suitable for writing large lines separated by pipes.

Right now I'm not in a need to write XML directly, but I also shall read about it. I don't want to remain in doubt about it.

Saludos from México City !!!
Maciej Los 6-Mar-18 11:09am
Hello, Miguel!

Saludos from Bialystok City!
Miguel Altamirano Morales 6-Mar-18 12:20pm
Maciej: I just loaded a file to our vendor's portal and they are telling me this one was OK. It is coded with UTF8 Encoding and doesn't carry a BOM.

I achieved that with the next code in my VB Net:

Private Sub Graba_Texto()
Dim writer As New System.IO.StreamWriter(strFileName, OpenMode.Append, System.Text.Encoding.UTF8)
End Sub

'strWork' is the line my program previously arranged with the necessary data for our PAC to digitally firm our Electronic invoice.

I really appreciate this new hint you gave me, it was very useful for me to achieve this goal.

I still can´t see very clearly the point about the class UTF8Encoding; as you see, I didn't have to use it. But I'm going to read it more carefully, along with the other topics you suggested me. I have to take advantage of being in contact with people like you, for intellectual purposes and personal improvement.

Thank you very much one more time, Dear Friend.
Maciej Los 6-Mar-18 13:59pm
You're very welcome, my Friend.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900