从VB net创建一个编码为UTF-8的文本文件 [英] Create a text file with encoding UTF-8 from VB net

查看:385
本文介绍了从VB net创建一个编码为UTF-8的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好。



我正在为我们的电子发票供应商生成一个文本文件,以数字方式确定并生成XML和PDF文件我工作的公司。



我在VB NET 2017中使用FileOpen命令,我曾经使用VB 6.0和VB Net 2010几年以前,事实上,我正在正确创建输出文本文件。



这是代码:



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
    System.IO.File.Delete(strFileName)
 End If
 FileOpen(1, strFileName, OpenMode.Output)



我正在使用打印语句逐一编写所需的行。



现在要点是我们的供应商希望文件包含UTF-8并避免BOM。



我已经读过这个编码字符并且避免BOM是一个问题构建HTML和XML文件,但我从未尝试过在文本文件中实现这一点,特别是通过VB net 2017生成它。



我看到我可以通过笔记本编辑文件并使用此UTF-8编码保存,但我需要通过vB网络代码进行保存。



我真的很感激任何帮助,或建议如何做到这一点或在哪里寻找有关它的信息。



非常感谢。



我尝试了什么:



我正在通过互联网搜索,试图找到一种方法来做到这一点。


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.

推荐答案

你必须使用StreamWriter [ ^ ]支持编码的类。





关于没有BOM的UTF-8 ...请阅读关于 Encoding.UTF8属性(System.Text) [ ^ ]

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)[^]
Quote:

要实例化不提供BOM的UTF8编码,请调用UTF8Encoding构造函数的任何重载。





所以,我建议阅读UTF8Encoding构造函数(布尔值,布尔值)(System.Text) [ ^ ]参数设置为 false 并且 true



[/ EDIT]



注意:如果您想使用数据进行进一步处理,可以直接将其写入xml。

请参阅:

XML Serialization - Tips&技巧 [ ^ ]

XML序列化和反序列化:第1部分 [ ^ ]

XML序列化和反序列化:第2部分 [ ^ ]

自定义类集合序列化的完整示例反序列化 [ ^ ]



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

[/EDIT]

Note: if you want to use data for further processing, you can write it into xml directly.
See:
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[^]


这篇关于从VB net创建一个编码为UTF-8的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆