C#中 如何修改一个已有的xml文件并保存为指定的编码

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="appConfigManage" type="RdClientManage" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
<add key="ServerIP" value="192.168.5.5" />
<add key="ServerPort" value="5556" />
<add key="CustomerName" value="hospital" />
<add key="EnglishName" value="ShanDongSQ IT Co.LTD" />
<add key="version" value="Copyright ©2014 RDHIS. All rights reserved." />
<add key="SkinStyle" value="Office 2007 Blue" />
</appSettings>
。。。。。。。。。。

默认编码为ansi
我用xmldocument 修改后
save完成编码竟然便成utf-8了
有没有方法可以save 指定的编码保存。
我就是想修改key= serverIP 的 value 的值。
有其他可用的类也行啊。各位兄弟们帮帮忙。。。

XML文件是按照其提供的信息“iso-8859-1”进行编码的!并采用默认的编码,并用简单的文本流处理方式,对结果重新编码后存入。xml文件的编码方式虽然标记为“iso-8859-1”,实际上却是GB2312或者其它。
代码如下:

List<string> xmlStr = new List<string>();
using (StreamReader sr = new StreamReader(srcFileName, System.Text.Encoding.Default))
{
while (sr.Peek() >= 0)
{
string line = sr.ReadLine();
xmlStr.Add(line);//原文件较大,且存在语法错误,需要检测修改,故分行保存
}
sr.Close();
}
using (StreamWriter sw = new StreamWriter(desFileName, true, System.Text.Encoding.UTF8))
{
foreach (string t in xmlStr)
{
sw.WriteLine(t);
}
sw.Close();
}
温馨提示:答案为网友推荐,仅供参考
相似回答