我需要以下配置文件:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ApplicationLauncher.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="OAuth2Service.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- Note that the paths can be entered on separate lines to ease readability
but no spaces are allowed as this will cause any path after the space to be hidden -->
<probing privatePath="Common;
Installed Providers\Messaging;
Installed Providers\CommonControls;
Installed Providers\DataManager"/>
</assemblyBinding>
</runtime>
<userSettings>
<Messaging.Properties.Settings>
<setting name="IsMessagingFeatureOn" serializeAs="String">
<value>True</value>
</setting>
</.Properties.Settings>
</userSettings>
</configuration>
我想更新以下内容的值:
<setting name="IsMessagingFeatureOn" serializeAs="String">
<value>True</value>
</setting>
为假。 以下是我用来更改值的代码:
string settingsFilePath = "XtApplicationLauncher.exe.config";
XmlDocument installerConfig = new XmlDocument();
installerConfig.PreserveWhitespace = true;
installerConfig.Load(settingsFilePath);
try
{
XmlElement machineName = (XmlElement)installerConfig.SelectSingleNode("//configuration/userSettings/Messaging.Properties.Settings/setting[@name='IsMessagingFeatureOn']/value");
if (machineName != null)
{
machineName.InnerText = "False";
}
installerConfig.Save(settingsFilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
值已更改,但问题是当我保存文件时,会添加以下字符:


该文件如下:
<probing privatePath="Common;
Installed Providers\Messaging;

这让我疯了。我该怎么做才能编写一个干净的文件并保持文件格式化。
答案 0 :(得分:0)


是XML格式的回车和换行符,因此如果您想使用XMLDocument
编写带有
<value>
3
</value>
这将变成
<value>
3</value>
所以解决方案是更新输入XML内容而不用像
这样的换行符<value>3</value>.
在你的情况下,它将是
<probing privatePath="Common;\Installed Providers\Messaging;Installed Providers\CommonControls;Installed Providers\DataManager;" />
另外,我看到你的标签没有正确关闭。