BOM of XML

Source: Internet
Author: User
Tags ultraedit

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.