如何修复“配置系统初始化失败/根元素丢失”;加载配置文件时出错
本文关键字:加载 出错 配置文件 元素 配置 何修复 系统 初始化 失败 | 更新日期: 2023-09-27 17:49:56
我在c# windows应用程序中得到了这个错误:"配置系统初始化失败"。
工作正常。突然我得到了一个例外。它将内部异常细节显示为"根元素缺失"。用户(C: ' '公司当地' AppData ' ' Clickbase_Corp_Sverige_AB ' TouchStation.vshost.exe_Url_no1nets4fg3oy2p2q2pnwgulbvczlv33 ' 1.1.0.12 ' user.config)"}。当我尝试从Settings.cs类中获取值时,会发生这种情况。
在program.cs文件中,下面的代码写为
if (Properties.Settings.Default.CallUpgrade)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.CallUpgrade = false;
Properties.Settings.Default.Save();
}
并调用settings.cs类,下面的代码抛出上面的异常
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool CallUpgrade {
get {
return ((bool)(this["CallUpgrade"]));
}
set {
this["CallUpgrade"] = value;
}
}
下面是我的整个app.config
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="TouchStation.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="TouchStation.TouchStation" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="TouchStation.TouchStation" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<section name="SitesInfo" type="TouchServer.SitesInfoSectionHandler,TouchServerLib" />
</configSections>
<appSettings>
<add key="WebRoot" value="webroot" />
<add key="TempDir" value="temp" />
<add key="ServerPort" value="9338" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<userSettings>
<TouchStation.Properties.Settings>
<setting name="Site" serializeAs="String">
<value />
</setting>
<setting name="StationID" serializeAs="String">
<value>0</value>
</setting>
<setting name="Location" serializeAs="String">
<value />
</setting>
<setting name="ShutdownTime" serializeAs="String">
<value>0000</value>
</setting>
<setting name="ReportStatusEvery" serializeAs="String">
<value>0</value>
</setting>
<setting name="SynchronizeEvery" serializeAs="String">
<value>10</value>
</setting>
<setting name="DefaultUsername" serializeAs="String">
<value />
</setting>
<setting name="DefaultPassword" serializeAs="String">
<value />
</setting>
<setting name="WatchdogTimeout" serializeAs="String">
<value>60</value>
</setting>
<setting name="RebootOnTimeout" serializeAs="String">
<value>False</value>
</setting>
<setting name="AnonymousLogin" serializeAs="String">
<value>True</value>
</setting>
<setting name="RefID" serializeAs="String">
<value />
</setting>
<setting name="AutoStart" serializeAs="String">
<value>False</value>
</setting>
<setting name="DemoMode" serializeAs="String">
<value>True</value>
</setting>
<setting name="UnlockPassword" serializeAs="String">
<value>needle</value>
</setting>
<setting name="SynchronizerUsername" serializeAs="String">
<value />
</setting>
<setting name="SynchronizerPassword" serializeAs="String">
<value />
</setting>
<setting name="RunClientApplications" serializeAs="String">
<value>False</value>
</setting>
<setting name="MapID" serializeAs="String">
<value>0</value>
</setting>
<setting name="ServerName" serializeAs="String">
<value />
</setting>
<setting name="CallUpgrade" serializeAs="String">
<value>True</value>
</setting>
<setting name="ServerPort" serializeAs="String">
<value>9338</value>
</setting>
</TouchStation.Properties.Settings>
<TouchStation.TouchStation>
<setting name="ServerURL" serializeAs="String">
<value />
</setting>
<setting name="Site" serializeAs="String">
<value />
</setting>
<setting name="StationID" serializeAs="String">
<value>0</value>
</setting>
<setting name="Location" serializeAs="String">
<value />
</setting>
<setting name="ShutdownTime" serializeAs="String">
<value />
</setting>
<setting name="ReportStatusEvery" serializeAs="String">
<value>0</value>
</setting>
<setting name="SynchronizeEvery" serializeAs="String">
<value>0</value>
</setting>
<setting name="HideMouse" serializeAs="String">
<value>False</value>
</setting>
<setting name="HideDesktopOnStart" serializeAs="String">
<value>False</value>
</setting>
<setting name="DefaultUsername" serializeAs="String">
<value />
</setting>
<setting name="DefaultPassword" serializeAs="String">
<value />
</setting>
<setting name="LogServerPort" serializeAs="String">
<value>9050</value>
</setting>
<setting name="WatchdogTimeout" serializeAs="String">
<value>60</value>
</setting>
<setting name="RebootOnTimeout" serializeAs="String">
<value>False</value>
</setting>
<setting name="AnonymousLogin" serializeAs="String">
<value>True</value>
</setting>
<setting name="RefID" serializeAs="String">
<value />
</setting>
</TouchStation.TouchStation>
</userSettings>
<applicationSettings>
<TouchStation.TouchStation>
<setting name="ClientSettingsURL" serializeAs="String">
<value />
</setting>
</TouchStation.TouchStation>
</applicationSettings>
<SitesInfo>
<sites>
<site Name="Local" FullName="Local Site" DatabaseConnectionString="Data'local.db" />
</sites>
</SitesInfo>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
有谁能帮我一下吗? 谢谢。
问候,珍妮
除了Akram Shahda的回答:我有同样的问题(配置系统初始化失败/根元素丢失)。AppData文件夹中的.config文件为空。这个问题通过删除AppData文件夹中的.config文件解决了。
XmlException
标题根元素缺失的原因意味着您试图加载的XML文档(这里的配置文件)格式不正确,更确切地说,它缺少根节点。
每个XML文件必须有一个包含所有其他元素的根元素/节点。
你的文件必须像下面这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<userSettings>
<WindowsFormsApplication.Properties.Settings>
</WindowsFormsApplication.Properties.Settings>
</userSettings>
</configuration>
以防有人读到:
对我来说,我解决了这个问题,只是去用户。配置位置,对于这个问题,它是:
(C: '用户当地公司' ' AppData ' ' Clickbase_Corp_Sverige_AB ' TouchStation.vshost.exe_Url_no1nets4fg3oy2p2q2pnwgulbvczlv33 ' 1.1.0.12 ' user.config)"
我删除了配置文件,重新启动了visual studio,它工作了。
希望这对你有帮助!
谢谢
在控制台应用程序中,你应该分配<<后的启动/>段;configSections/>
一般来说,"Configuration system failed to initialize"问题很可能是由app.config中无效的XML结构引起的。当我试图将代码从"测试床"配置文件移植到服务app.config文件时,我经常会遇到这种情况。
在我的例子中,connectionStrings节点声明了两次。
我们的一个客户在尝试从c#项目FTP时遇到了这个错误"Configuration system failed to initialize"。该用户不是管理员。
我们的技术客户调查并发现,在非管理员帐户下使用ProcMon,该进程使用的资源是。net机器。配置文件。(c: ' windows ' microsoft.net ' framework ' v4.0.30319 ' config ' machine.config)
我们的客户只授予本地用户对这个配置文件的修改权限,这立即解决了问题。(他们不允许用户成为管理员,所以这是必要的。)
如果上面的答案不适合你,也许这个会有帮助。
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
con = new OleDbConnection(config.ConnectionStrings.ConnectionStrings["connectionStr"].ConnectionString.ToString());
在我的情况下,检查完所有内容后,问题出现在App.config的最后一部分,其中Log4Net配置语句产生错误。
删除App.config中所有额外的内容并开始调试
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="[Application Name].Settings.Client" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<applicationSettings>
<[Application Name].Settings.Client>
<setting name="IDClient" serializeAs="String">
<value>6</value>
</setting>
</[Application Name].Settings.Client>
</applicationSettings>
</configuration>
.Net Core with Graphviz:
<configSections> <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > </sectionGroup> </configSections> <appSettings> <add key="graphVizLocation" value="C:'Program Files (x86)'Graphviz2.38'bin" /> </appSettings> </configuration>
我今天遇到了同样的问题。我试图编译一个visual studio解决方案,但无法。
问题是配置文件配置了xml名称空间。Visual studio/msbuild多年来一直在应用程序配置中包含这个命名空间,这使得这个错误有点奇怪。
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
但是一旦删除了命名空间和visual studio/msbuild就又高兴了。
<configuration>
我取了用户的副本。配置文件,将其放在资源下,并在应用程序中使用默认值进行备份。如果用户。配置文件不在它应该在的地方,我通过创建这个文件并输入它来解决这个问题。
if (!File.Exists(ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath)) //user.config path
{
using (XmlWriter writer = XmlWriter.Create(ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath)) //configuration root is added
{
writer.WriteStartElement("configuration");
writer.WriteEndElement();
writer.Flush();
}
File.WriteAllText(ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath, Properties.Resources.user);
Application.Restart(); //restart application
Environment.Exit(0);
}