简单问题:如何在xml的声明部分加入encoding内容

lbl20020123 2004-08-30 05:26:15
xml的声明部分一般由三部分构成,version,encoding和standalone。我现在有一个xml文件,我要根据他的内容生成另外一个xml文件。
新文件的声明部分只有version,请问:如何在程序中加入encoding内容??
...全文
125 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2004-08-31
  • 打赏
  • 举报
回复
the above suggetion is a hack, you should let the writer to output the right encoding, for example, try something like


using System;
using System.Xml;

class TestWriteXml
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root><child>1</child><child>2</child></root>");

XmlTextWriter xtw = new XmlTextWriter("myfile.xml",System.Text.Encoding.GetEncoding("GB2312"));
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
doc.Save(xtw);
//the following is also fine
//xtw.WriteNode(new XmlNodeReader(doc.DocumentElement), true);
xtw.WriteEndDocument();
xtw.Close();
}
}
孟子E章 2004-08-30
  • 打赏
  • 举报
回复
你用用File FileInfo写入文件

str = "<?xml version='1.0' encoding='gb2312'?>" + xmlDom.OuterXml;
StreamWrite w = new .....
w.Write(str)

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧