Recently made an XML generation and import function, generated XML after syntax verification and schema verification, no error, encoding method is also required UTF-8, but the import has been prompted:
Error on line 1 of document: content is not allowed in Prolog. nested exception: content is not allowed in Prolog.
The Tax Department's website cannot be viewed during the import of the system.Code, Trace exception, all information is only this inexplicable error prompt.
After searching, it is found that it may be caused by BOM.
What is Bom?
Bom: byte order mark, which is a byte sequential mark of Chinese names. We recommend that you transmit the BOM to determine the byte sequence before transmitting the byte stream. In fact, UTF-8 does not need to use BOM to indicate the byte order, but BOM can be used to indicate the encoding method. Bom UTF-8 encoding is ef bb bf, so if the recipient receives a byte stream starting with ef bb bf, it indicates it is UTF-8 encoding.
It can be seen that for the UTF-8, Bom is dispensable, but some XML parsing method does not recognize this Bom, so it reported an error.
How can we determine whether XML contains Bom?
Open XML with ultraedit or other software that can have the hexadecimal editing function. If ef bb bf is started, it indicates that Bom is included.
How to remove Bom?
You can open XML with ultraedit or emeditor, and save it as an option to save it as a UTF-8 without BOM or as a UTF-8 with Bom.
How to generate xml without Bom?
To remove the generated XML Bom, it must be resolved from the source. It is impossible for the user to manually edit the BOM and import it after each XML generation.
The previously generated XML code is:
Xmlwritersettings settings = New Xmlwritersettings ();
Settings. Encoding = System. Text. encoding. getencoding ( " UTF-8 " );
Xmlwriter writer = Xmlwriter. Create (pathname, settings );
After modification:
Xmlwritersettings settings = New Xmlwritersettings ();
Settings. Encoding = New Utf8encoding ( False );
Xmlwriter writer = Xmlwriter. Create (pathname, settings );
In this way, the XML generated using xmlwriter does not contain Bom. The import is successful and no error is reported.