由于XML很小,偶用XmlDocument来生成
XmlDocument xmldoc = new XmlDocument();
XmlNode nodePI = xmldoc.CreateProcessingInstruction("xml","version=\"1.0\" encoding=\"gb2312\"");
...//生成XML节点
xmldoc.Save("test.xml");在代码中,XML的文档的encoding指定为gb2312,但保存XML文件时,文件的编码却是UTF-8,二者完全不同,如果XML含有中文,就不行了。怎样指定XML文件保存的编码?
XmlDocument xmldoc = new XmlDocument();
XmlNode nodePI = xmldoc.CreateProcessingInstruction("xml","version=\"1.0\" encoding=\"gb2312\"");
...//生成XML节点
xmldoc.Save("test.xml");在代码中,XML的文档的encoding指定为gb2312,但保存XML文件时,文件的编码却是UTF-8,二者完全不同,如果XML含有中文,就不行了。怎样指定XML文件保存的编码?
解决方案 »
- CSharp 界面刷新 Thread.Sleep()导致界面假死怎么解决?
- winfrom combobox能绑定多个tag值吗?我要每个combobox的value有个tab值
- 如何取消转义字符
- 求三层架构模式的网吧管理源代码
- C# windows 应用程序中怎么更新datagridview的显示内容?
- C#编Windows应用程序,如何取得屏幕的象素大小?
- 未知sql查询语句的情况下怎么获得这条语句访问的表名?
- 如何在winform中怎么引用Excel9.0 lib,把DataGird中的数据用循环赋值到生成的文件Excel中的单元格,在线等待结贴!,(附参考代码)
- 关于DataGridView的问题(急!)
- 边个知到,哪个网可以。"中国象棋"在网页中对战的啊?
- 有个问题要提,我一个解决方案里有多个项目,怎么解决?
- 运用记录集,读取TXT有误,请大家帮帮忙!
using System.IO;
using System.Xml;
public class Sample { public static void Main() {
// Create and load the XML document.
XmlDocument doc = new XmlDocument();
string xmlString = "<book><title>Oberon's Legacy</title></book>";
doc.Load(new StringReader(xmlString));
// Create an XML declaration.
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
xmldecl.Encoding="UTF-8";
xmldecl.Standalone="yes";
// Add the new node to the document.
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
// Display the modified XML document
Console.WriteLine(doc.OuterXml);
}
}
XmlTextWriter xtr=new XmlTextWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
xtr.Formatting=Formatting.Indented;
xtr.Indentation=0;
xtr.WriteStartDocument();
ds.WriteXml(xtr);
xtr.Close();
fs.Close();