C# - 打开 Settings.settings 会收到有关 app.config 中无效 xml 的错误(添加 configSource 时)
根据大量示例,我将以下内容添加到我的 app.config 文件中:
当我运行应用程序时,一切似乎都正常,但当我尝试打开 Settings.settings 文件时,我收到错误:
“读取 app.config 文件时发生错误。该文件可能已损坏或包含无效的 XML。”
Settings.settings 文件打开,但如果我尝试保存它,我会收到类似的错误消息。
应用程序.config文件:
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="test.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<test.Properties.Settings>
<setting name="server" serializeAs="String">
<value />
</setting>
<setting name="database" serializeAs="String">
<value />
</setting>
<setting name="g_language" serializeAs="String">
<value>en-US</value>
</setting>
<setting name="timeSchedule1" serializeAs="String">
<value />
</setting>
<setting name="timeSchedule2" serializeAs="String">
<value />
</setting>
<setting name="helpLocation" serializeAs="String">
<value />
</setting>
<setting name="SQLAuthType" serializeAs="String">
<value>0</value>
</setting>
<setting name="SQLLogin" serializeAs="String">
<value />
</setting>
<setting name="SQLPsw" serializeAs="String">
<value />
</setting>
<setting name="defaultTimeZone" serializeAs="String">
<value />
</setting>
</test.Properties.Settings>
</userSettings>
<connectionStrings configSource = "testConnect.config"/>
Following numerous examples, I added the following to my app.config file:
Everything seems to work when I run the application but when I try to open the Settings.settings file, I get the error:
"An error occurred while reading the app.config file. The file might be corrupted or contain invalid XML."
The Settings.settings file opens but I get a similar error message if I try to save it.
App.config file:
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="test.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<test.Properties.Settings>
<setting name="server" serializeAs="String">
<value />
</setting>
<setting name="database" serializeAs="String">
<value />
</setting>
<setting name="g_language" serializeAs="String">
<value>en-US</value>
</setting>
<setting name="timeSchedule1" serializeAs="String">
<value />
</setting>
<setting name="timeSchedule2" serializeAs="String">
<value />
</setting>
<setting name="helpLocation" serializeAs="String">
<value />
</setting>
<setting name="SQLAuthType" serializeAs="String">
<value>0</value>
</setting>
<setting name="SQLLogin" serializeAs="String">
<value />
</setting>
<setting name="SQLPsw" serializeAs="String">
<value />
</setting>
<setting name="defaultTimeZone" serializeAs="String">
<value />
</setting>
</test.Properties.Settings>
</userSettings>
<connectionStrings configSource = "testConnect.config"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
当 app.Config 文件包含语法正确但逻辑上不正确的元素时,可能会生成此错误。
示例:我刚刚收到一个正在生成的项目当您尝试打开 Settings.settings 文件时出现同样的错误:
确保 XML 文档在语法上正确后(使用 http://validator.w3.org ),我删除了 XML 元素,直到错误消失。
在我的例子中,app.config 文件包含两个 connectionStrings 元素:
GENERATES ERROR
“无错误生成”
It appears that this error can be generated when the app.Config file contains elements that are syntactically correct, but logically incorrect.
Example: I was just handed a project that was generating the same error when you attempt to open the Settings.settings file:
After ensuring that the XML document was syntactically correct (using http://validator.w3.org), I remove XML elements until the error disappeared.
In my case, the app.config file contained two connectionStrings elements:
GENERATES ERROR
NO ERROR GENERATED
我有同样的问题。和您一样,我在一个单独的文件中有连接字符串:
除了在使用设置时删除元素之外,我找不到解决方法。
I have the same problem. As you, I have connection strings in a separate file:
I have not been able find a work around, other than removing the element while working with settings.
我也在
connectionStrings
元素中使用configSource
。我认为我遇到的问题是我引用的包含的配置文件具有相对于构建目标目录的路径(例如 bin\debug\ConnectionStrings.config)。我已将 ConnectionStrings.config 链接项设置为复制到构建时的输出目录。但是,VS IDE 希望在从app.settings
而不是bin\debug\program.exe.settings
构建之前解决它,所以我要么有一个副本在每个项目中使用这个额外的配置文件(这会消除好处),或者使用完全限定的路径。如果我要使用后一种方法,我需要用某种环境变量来表达路径,以便配置文件可以在开发计算机和部署计算机上运行。这可能可行,但看起来.NET 实际上不会扩展环境变量。如果有帮助,您可以尝试根据架构验证 app.config,如 https://stackoverflow.com/a/311835/611672 中所述。就我而言,xml 通过了验证,但我仍然收到错误消息。
此外,如果有人在使用此设置添加 Code First 迁移时遇到问题(出现找不到 connectionStrings.config 的错误),这可能是相同的根本原因。
I'm using
configSource
in theconnectionStrings
element as well. I think the issue I had was that I was referring to the included config file with a path relative to the build target directory (e.g. bin\debug\ConnectionStrings.config). I've got my ConnectionStrings.config linked item set to copy to the output directory on build. However, the VS IDE wants to resolve it before build fromapp.settings
instead ofbin\debug\program.exe.settings
, so I'm stuck either having a copy of this extra config file in every project (which removes the benefit), or using a fully qualified path. If I'm to use the latter approach, I'd need to express the path in terms of some kind of environment variable so that the config file would work on a development machine and the deployment machine. That might be doable, but it looks like .NET won't actually expand the environment variables.If it helps, you can try validating app.config against the schema as described at https://stackoverflow.com/a/311835/611672. In my case, the xml passes validation but I still get the error message.
Also, if anyone is having trouble adding a Code First Migration with this setup (getting an error that it can't find connectionStrings.config), this is probably the same root cause.
我在手动重命名解决方案(项目和命名空间)时遇到了同样的错误。
该错误是由 App.config 文件的
节点中重复的元素引起的。我不确定什么操作导致创建重复元素,但解决方案是删除重复元素。
I encountered the same error while manually renaming a solution, it's projects and namespaces.
The error was caused by duplicate
<section>
elements in the<sectionGroup name="applicationSettings">
node of the App.config file. I'm not sure what action caused the duplicate elements to be created but the solution was to remove the duplicate element.我一直在摆弄我的 Settings.settings 文件和 app.config 文件。我没有找到任何重复的节点,但我已经移动了代表 app.config 中设置的一些父节点的顺序。我只是不喜欢他们的顺序,并认为我可以在 app.config 中即时更改它。我错了。
我没有发现这是问题所在,直到我最终删除了 app.config,更改了 Settings.settings 中的设置并保存/清理了解决方案/重建解决方案。然后我能够将新生成的 app.config 与之前出错的 app.config 进行比较。
I had been messing around with both my Settings.settings file and my app.config file. I did not find any duplicate nodes, but I had moved the order of some parent nodes representing settings around in my app.config. I just didn't like their order and thought I could change it on the fly in app.config. I was wrong.
I did not discover that this was the problem was until I finally deleted app.config, changed a setting in Settings.settings and saved/cleaned solution/rebuild solution. Then I was able to compare my newly generated app.config with previous one that was erroring out.
听起来您的应用程序配置文件配置不正确,您可以发布您的应用程序配置吗?
It sounds like your app config file is not properly configured, could you post you app config?
我也刚刚遇到这个问题。看来这可能是由 app.config 文件中的多个“奇怪之处”引起的。
我解决这个问题的方法:
如果仍然出现错误,请开始逐一删除部分,直到找到重复/有问题的部分。
I also just had this problem. It seems it can be caused by multiple "oddities" within your app.config file.
The way I solved it:
If you still get the error, start removing sections one by one until you find the duplicate / offending section.
就我而言,我有很多连接字符串,并且
中的字符串之一为空。我也遇到了与 Pressacco 提到的相同的错误。谢谢!
In my case I had lot of connection strings and one of the string in
<add name>
was empty.I also had the same error as Pressacco mentioned. Thanks!
我用拐杖走过这条路已经太晚了,但即使我遇到了同样的问题,尽管 app.config 中的一切看起来都是正确的,但我收到了这个错误。
将一些属性设置添加到 app.config 文件后,我尝试编辑/打开 Settings.settings 文件,这时我收到了此错误。
我所做的是,我恢复了对 app.config 所做的所有更改,并将所有必需的值直接添加到设置文件中,这解决了问题。
直接向 app.config 添加值会产生问题。
I cane across this way too late but even I had the same issue and though everything looked correct in the app.config, I was getting this error.
After adding some property settings to the app.config file, I was trying to edit/open the Settings.settings file and that's when I got this error.
What I did was, I reverted all changes made to the app.config and added all required values directly to the settings file,that resolved the issue.
Directly adding values to app.config was creating the problem.
我注意到这种情况经常发生在我身上,这是由于将 Oracle.DataAccess.dll 替换为 Oracle.ManagedDataAccess.dll 造成的。这将创建一个配置节,如下所示:
但我已经在 configSections 中定义了一个sectionGroup,因此两者不能共存。我通过向
oracle.managementdataaccess.client
添加一个组来解决,然后进行分组,然后根据需要应用到我的配置中。花了很长时间才找到的问题的简单解决方案!
I noticed this had been happening to me quite often and was due to switching out Oracle.DataAccess.dll for Oracle.ManagedDataAccess.dll. This creates a config section as shown:
But I already have a sectionGroup defined in
configSections
so the two cannot coexist. I resolved by adding a group to theoracle.manageddataaccess.client
and then grouped then applied in my config as needed.Simple solution for a issue that took way too long to find!