开发者

The root element must match the name of the section referencing the file

开发者 https://www.devze.com 2023-02-02 04:05 出处:网络
I am using the \"File\"attribute of AppSettings but I am getting this error The root element must match the name

I am using the "File" attribute of AppSettings but I am getting this error

The root element must match the name of the section referencing the file, 'appSettings'

Well I am adding a ClassLibrary1.dll and ClassLibrary1.dll.config in my windows project. Ny windows application is having its own app.config

<configuration>
 <appSettings file="ClassLibrary1.dll.config"> 
    <add key="main" value="main"/>
  </appSettings>
</configuration>

thankx开发者_JAVA技巧 in advance


Your external file must look like:

<?xml version="1.0"?>
<appSettings>
  <add key="main" value="main"/>
</appSettings>

Unlike when you use configSource, you can't have a configuration node when using the file attribute. I'm not even 100% sure you can use a configuration node with the configSource attribute, I always match the root node that is being externalized. See the MSDN documentation for more information: http://msdn.microsoft.com/en-us/library/ms228154.aspx

Also, I've never tried to reference an assembly configuration file as you are here. I wonder if that may be causing problems? Try extracting just the appSettings node to another config file and see if that solves the issue.

edit: This external file (we'll call it appSettings_external.config) can be used in two ways:

app.config: (settings are merged)

   <?xml version="1.0"?>
   <configuration>
    <appSettings file="appSettings_external.config">
      <add key="main" value="main"/>
    </appSettings>
   </configuration>

app.config: (settings are pulled only from external config)

   <?xml version="1.0"?>
   <configuration>
    <appSettings configSource="appSettings_external.config" />
   </configuration>


Make sure that the root XML element in your file "ClassLibrary1.dll.config" is "appSettings".

http://weblogs.asp.net/pwilson/archive/2003/04/09/5261.aspx


Here is my follow-up answer based on the OP's recent comment: What if I have more than one external config file?

My suggestion is to revise the design of the configuration settings. I assume that you have multiple class libraries, and each class library has its own set of settings that are currently being stored in the appSettings section. In that case, your config file probably looks something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- App Settings for Class Library 1 -->
    <add key="ClassLibrary1Value1" value="123"/>
    <add key="ClassLibrary1Value2" value="234"/>
    <add key="ClassLibrary1Value3" value="345"/>
    <!-- App Settings for Class Library 2 -->
    <add key="ClassLibrary2Value1" value="ABC"/>
    <add key="ClassLibrary2Value2" value="BCD"/>
    <add key="ClassLibrary2Value3" value="CDE"/>
  </appSettings>
</configuration>

and your code for accessing those settings might look something like this:

        var appSettings = System.Configuration.ConfigurationManager.AppSettings;
        Console.WriteLine(appSettings["ClassLibrary1Value1"]);
        Console.WriteLine(appSettings["ClassLibrary1Value2"]);
        Console.WriteLine(appSettings["ClassLibrary1Value3"]);

        Console.WriteLine(appSettings["ClassLibrary2Value1"]);
        Console.WriteLine(appSettings["ClassLibrary2Value2"]);
        Console.WriteLine(appSettings["ClassLibrary2Value3"]);

Instead, I suggest you try separating each class library's settings into its own config section. You could accomplish this by modifying your executable's config file to look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="classLibrary1" type="System.Configuration.AppSettingsSection"/>
    <section name="classLibrary2" type="System.Configuration.AppSettingsSection"/>
  </configSections>
  <classLibrary1 configSource="ClassLibrary1.dll.config" />
  <classLibrary2 configSource="ClassLibrary2.dll.config" />
</configuration>

Your ClassLibrary1.dll.config file would look like this:

<?xml version="1.0" encoding="utf-8" ?>
<classLibrary1>
  <add key="Value1" value="123"/>
  <add key="Value2" value="234"/>
  <add key="Value3" value="345"/>
</classLibrary1>

Your ClassLibrary2.dll.config file would look like this:

<?xml version="1.0" encoding="utf-8" ?>
<classLibrary2>
  <add key="Value1" value="ABC"/>
  <add key="Value2" value="BCD"/>
  <add key="Value3" value="CDE"/>
</classLibrary2>

and your code to access these settings would look something like this:

        var classLibrary1AppSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("classLibrary1");
        Console.WriteLine(classLibrary1AppSettings["Value1"]);
        Console.WriteLine(classLibrary1AppSettings["Value2"]);
        Console.WriteLine(classLibrary1AppSettings["Value3"]);

        var classLibrary2AppSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("classLibrary2");
        Console.WriteLine(classLibrary2AppSettings["Value1"]);
        Console.WriteLine(classLibrary2AppSettings["Value2"]);
        Console.WriteLine(classLibrary2AppSettings["Value3"]);


The / chars of the end tags seem to be missing:

<configuration>
    <appSettings file="ClassLibrary1.dll.config"> 
        <add key="main" value="main" />
    </appSettings>
</configuration>


have a look on the first result that google give you when you type "The root element must match the name of the section referencing the file"

http://weblogs.asp.net/pwilson/archive/2003/04/09/5261.aspx

then look for "The root element must match the name of the section referencing the file" in the page


Apart from making sure that your external config file does fulfill the requirement specified in the error message (i.e. the root element of the xml should be appSettings) you can also check whether VS isn't reading the old version. When you modify the external config file no recompilation is triggered, so for each change you should also modify the main web.config so that it gets refreshed (or at least touch it) .

0

精彩评论

暂无评论...
验证码 换一张
取 消