Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Opening Settings.settings gets error about invalid xml in app.config (when configSource added)

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"/>

like image 729
Pat Avatar asked Sep 01 '11 14:09

Pat


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


3 Answers

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.

like image 127
Alf Kåre Lefdal Avatar answered Oct 25 '22 19:10

Alf Kåre Lefdal


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.

like image 29
Jay Carlton Avatar answered Oct 25 '22 20:10

Jay Carlton


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

So it appears that this error can be generated when the app.Config file contains elements that are syntactically correct, but logically incorrect.

like image 37
Pressacco Avatar answered Oct 25 '22 19:10

Pressacco