XML cannot show Chinese, and UTF-8 cannot show Chinese

Source: Internet
Author: User

If your operating system is a Chinese system,

1. You create an XML document. The default value is gb2312.
<? XML version = "1.0" encoding = "gb2312"?> You can definitely display Chinese characters.

2, you are changing someone else's XML document, but before the XML document is UTF-8/16 encoding,
<? XML version = "1.0" encoding = "gb2312"?>
Cannot display Chinese, because this document is not gb2312 encoding, although you force declare it to use gb2312 encoding conversion, but the gb2312 encoding mechanism can not handle the UTF-8/16 encoding. Must be converted using a UTF-8/16 encoding mechanism. That is
<? XML version = "1.0" encoding = "UTF-8"?> Or simply does not declare the encoding attribute.

PS: You can use the Save as function of the notepad that comes with windows to perform encoding and conversion.

 

 

Using system;
Using system. xml;
Class testxml
{
Public static void main ()
{
String sfile = "A. xml ";
Xmldocument Doc = new xmldocument ();
Doc. Load (sfile );

Xmlprocessinginstruction newpi;
String pitext = "type = 'text/XSL 'href = 'book. XSL '";
Newpi = Doc. createprocessinginstruction ("XML-stylesheet", pitext );
Doc. insertbefore (newpi, Doc. firstchild );

Xmltextwriter xtw = new xmltextwriter (sfile, system. Text. encoding. getencoding ("gb2312 "));
Xtw. Formatting = formatting. indented;
Xtw. writestartdocument ();
Doc. Save (xtw );
Xtw. writeenddocument ();
Xtw. Close ();
}
}


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.