C# - 打开 Settings.settings 会收到有关 app.config 中无效 xml 的错误(添加 configSource 时)

发布于 2024-12-02 21:03:59 字数 2153 浏览 1 评论 0原文

根据大量示例,我将以下内容添加到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

疧_╮線 2024-12-09 21:03:59

当 app.Config 文件包含语法正确但逻辑上不正确的元素时,可能会生成此错误。

示例:我刚刚收到一个正在生成的项目当您尝试打开 Settings.settings 文件时出现同样的错误:

“读取 app.config 文件时发生错误。该文件可能
已损坏或包含无效的 XML。”

确保 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:

"An error occurred while reading the app.config file. The file might
be corrupted or contain invalid XML."

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

  • configuration
    • connectionStrings
      • add
    • connectionStrings
      • add

NO ERROR GENERATED

  • configuration
    • connectionStrings
      • add
      • add
萧瑟寒风 2024-12-09 21:03:59

我有同样的问题。和您一样,我在一个单独的文件中有连接字符串:

<connectionStrings configSource = "testConnect.config"/>

除了在使用设置时删除元素之外,我找不到解决方法。

I have the same problem. As you, I have connection strings in a separate file:

<connectionStrings configSource = "testConnect.config"/>

I have not been able find a work around, other than removing the element while working with settings.

歌枕肩 2024-12-09 21:03:59

我也在 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 the connectionStrings 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 from app.settings instead of bin\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.

谜泪 2024-12-09 21:03:59

我在手动重命名解决方案(项目和命名空间)时遇到了同样的错误。
该错误是由 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.

梦屿孤独相伴 2024-12-09 21:03:59

我一直在摆弄我的 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.

信愁 2024-12-09 21:03:59

听起来您的应用程序配置文件配置不正确,您可以发布您的应用程序配置吗?

It sounds like your app config file is not properly configured, could you post you app config?

谁许谁一生繁华 2024-12-09 21:03:59

我也刚刚遇到这个问题。看来这可能是由 app.config 文件中的多个“奇怪之处”引起的。

我解决这个问题的方法:

  1. 备份你的设置文件和.cs,还有你的app.config(以防万一)
  2. 删除引用设置的configSections元素,例如app.config中的userSettings从app.config中
  3. 删除applicationSettings或userSettings部分app.config
  4. 保存 app.config
  5. 打开设置编辑器并进行任何小的更改,然后保存

如果仍然出现错误,请开始逐一删除部分,直到找到重复/有问题的部分。

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:

  1. Make a backup copy of your settings file and .cs, also of your app.config (just in case)
  2. Remove the configSections elements referring to settings, e.g. userSettings from app.config
  3. Remove the applicationSettings or userSettings sections from app.config
  4. Save your app.config
  5. Open the settings editor and make any small change, save

If you still get the error, start removing sections one by one until you find the duplicate / offending section.

[浮城] 2024-12-09 21:03:59
<configuration>
  <connectionStrings>
    <add name=""
  </connectionStrings>
</configuration>

就我而言,我有很多连接字符串,并且 中的字符串之一为空。
我也遇到了与 Pressacco 提到的相同的错误。谢谢!

<configuration>
  <connectionStrings>
    <add name=""
  </connectionStrings>
</configuration>

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!

花之痕靓丽 2024-12-09 21:03:59

我用拐杖走过这条路已经太晚了,但即使我遇到了同样的问题,尽管 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.

樱花细雨 2024-12-09 21:03:59
  1. 在另一个 VS 解决方案上,创建一个与您正在处理的项目具有相同名称和命名空间的新项目。
  2. 在属性>>下新项目的设置,创建您正在处理的项目中的所有设置。
  3. 进入新项目文件夹,即 C:\Users\YourUser\Documents\Visual Studio 2022\Projects\ReportsApplication1\ReportsApplication1 并复制 app.config 文件
  4. 粘贴此文件并替换当前项目中的文件
  5. (完成)
  1. On another VS solution, create a new project with the same name and namespaces as the one you have been working on.
  2. Under Properties >> Settings for the new project, create all the settings as you have in the one you are working on.
  3. Go into the new project folder i.e. C:\Users\YourUser\Documents\Visual Studio 2022\Projects\ReportsApplication1\ReportsApplication1 and copy the app.config file
  4. Paste this file and replace the one in your current project
  5. (Done)
全部不再 2024-12-09 21:03:59

我注意到这种情况经常发生在我身上,这是由于将 Oracle.DataAccess.dll 替换为 Oracle.ManagedDataAccess.dll 造成的。这将创建一个配置节,如下所示:

<section name="oracle.manageddataaccess.client" 
    type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>

但我已经在 configSections 中定义了一个sectionGroup,因此两者不能共存。我通过向 oracle.managementdataaccess.client 添加一个组来解决,然后进行分组,然后根据需要应用到我的配置中。

<sectionGroup name="oracleClient">
    <section name="oracle.manageddataaccess.client"
        type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</sectionGroup>

花了很长时间才找到的问题的简单解决方案!

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:

<section name="oracle.manageddataaccess.client" 
    type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>

But I already have a sectionGroup defined in configSections so the two cannot coexist. I resolved by adding a group to the oracle.manageddataaccess.client and then grouped then applied in my config as needed.

<sectionGroup name="oracleClient">
    <section name="oracle.manageddataaccess.client"
        type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</sectionGroup>

Simple solution for a issue that took way too long to find!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文