错误 "The root element must match the name of the section referencing the file",当根元素与节的名称匹配时

Error "The root element must match the name of the section referencing the file", when root element does match the name of the section

我正在尝试使用 "file" 属性将 appSettings 配置部分移动到外部文件并不断收到错误消息:

System.Configuration.ConfigurationErrorsException : The root element must match the name of the section referencing the file, 'appSettings'

问题是外部文件中的根元素已经是 'appSettings'。 尝试使用 root 'configuration' 和自定义配置部分,但也没有帮助

外部文件Test.config在下面

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="URL" value="https://www.google.com" />
</appSettings>

App.config 文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow"/>
  </configSections>
  <specFlow>
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
  </specFlow>
  <appSettings file="Test.config"/>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>

谁能帮我解决这个问题?

谢谢。

对我来说这是一个路径问题。 visual studio 的默认行为是不将文件包含在输出目录中。我将每个构建的文件属性更改为 "Copy Always" 以确保您拥有最新的文件。如果您已将其添加到属性部分的项目中,则文件引用应在路径中包含属性文件夹。