【问题标题】:Preserve formatting for config file保留配置文件的格式
【发布时间】:2017-10-23 09:59:32
【问题描述】:

我需要以下配置文件:

<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);
            }

值已更改,但问题是当我保存文件时添加了以下字符:

&#xD;&#xA;

文件如下:

<probing privatePath="Common;&#xD;&#xA;Installed Providers\Messaging;&#xD;&#xA;

这让我发疯了。我该怎么做才能编写干净的文件并保持文件格式。

【问题讨论】:

  • 您的
  • 标签现已关闭

标签: c# xml xmldocument


【解决方案1】:

&amp;#xD;&amp;#xA; 是 XML 中的回车符和换行符,所以如果你想使用 XMLDocument 编写 XML 并输入类似

<value>  
  3 
</value>

这会变成

<value>&#xD;&#xA;3</value>

所以解决方案是更新输入的 XML 内容而不像

那样换行
<value>3</value>.

在你的情况下,它会是

<probing privatePath="Common;\Installed Providers\Messaging;Installed Providers\CommonControls;Installed Providers\DataManager;" />

另外,我看到你的标签没有正确关闭。

【讨论】:

    猜你喜欢
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2021-06-17
    • 1970-01-01
    • 2019-01-16
    相关资源
    最近更新 更多