如果你的操作系统是中文系统,
1.你新建xml文档,默认是gb2312编码的。
<?xml version="1.0" encoding="gb2312" ?> 绝对可以显示中文。
2,你是改的别人的xml文档,但之前该xml文档是UTF-8/16编码的,
<?xml version="1.0" encoding="gb2312" ?>
不能显示中文,因为该文档不是gb2312编码的,虽然你强制申明它要用gb2312编码转换,但gb2312编码机制处理不了UTF-8/16的编码。必须用UTF-8/16编码机制转换。也就是
<?xml version="1.0" encoding="UTF-8" ?> or 干脆不声明encoding属性。
PS:用windows自带的记事本的另存为功能,可以进行编码转换。
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();
}
}