Why im getting errors on this? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I created a new project added a webbrowser.
Then I went to the file App.config.
There were 3-4 lines there and I changed their content to this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="EnableSqlDependency" value="true" />
<add key="ApplicationId" value=""></add>
<YOUR APP="" ID="" GOES="" HERE="">
" />
<add key="ApplicationUrl" value="" />
<add key="ApiKey" value="" />
<add key="ApplicationSecret" value=""></add><YOUR APP="" SECRET="" GOES="" HERE=""></YOUR>
" />
<add key="ExtendedPermissions" value="offline_access" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
I'm getting now 3 errors:
Error 1 Application Configuration file "App.config" is invalid. The 'YOUR' start tag on line 6 position 6 does not match the end tag of 'appSettings'. Line 13, position 9.
Error 3 Expecting end tag .
Error 2 Tag was not closed.
How do i fix this erros ? Can someone show me a fixed code according to my App.config code ?
EDIT**
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="EnableSqlDependency" value="true" />
<add key="ApplicationId" value=""></add>
<YOUR APP="" ID="" GOES="" HERE="">
" />
<add key="ApplicationUrl" value="" />
<add key="ApiKey" value="" />
<add key="ApplicationSecret" value=""></add>
</YOUR>
"
<add key="ExtendedPermissions" value="offline_access" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
Warning 1 The element 'appSettings' has invalid child element 'YOUR'. List of possible elements expected: 'add, remove, clear'.

How do i fix this erros
Remove (or comment) the malformed XML that does not fit the web.config schema:
<!-- <YOUR APP="" ID="" GOES="" HERE="">
" /> -->

Your Config file is mal-formed. Copy below xml and paste-overwrite your current config file.
Then
- Where it says YOUR_APP_ID_GOES_HERE paste the APP_ID code you got from the API Provider / whichever API you are trying to use.
- Where it says YOU_APP_SECRET_GOES_HERE paste the SECRET CODE / KEY you got from the API Provider / whichever API you are trying to use.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="EnableSqlDependency" value="true" />
<add key="ApplicationId" value="YOUR_APP_ID_GOES_HERE"/>
<add key="ApplicationUrl" value="" />
<add key="ApiKey" value="" />
<add key="ApplicationSecret" value="YOU_APP_SECRET_GOES_HERE" />
<add key="ExtendedPermissions" value="offline_access" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
Final note: You'll need the ApiKey value also set to a valid string, in order to be able to use the API that you are trying to write a client for.

Related

How to solve App config error Configuration system failed to initialize

I came for advice.I'm trying to connect to the database through C#.But I have a problem with the App config file.It keeps throwing the App exception:,,
Configuration system failed to initialize
".I tried to add ,as advised here:Configuration System Failed To Initialize , but with no positive result.
So I wonder,if there is still a problem in the code,but I rather think that the problem is with the App config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="DataSource" value=" My Database'IP adress "/>
<add key="Database" value="My database Name"/>
<add key="Name" value="User name"/>
<add key="Password" value="Password"/>
</configuration>
It seems like you're missing the appSettings element that wraps your key-value collection. It should look more like the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DataSource" value=" My Database'IP adress "/>
<add key="Database" value="My database Name"/>
<add key="Name" value="User name"/>
<add key="Password" value="Password"/>
</appSettings>
</configuration>

Custom section in App.Config, ConfigurationManager stops working

I have created a console app in c# that reads information from App.config. if i add things in appSettings section, i can acces them and it works, but as soon as i add some custom sections i cant read anything from it. I am using ConfigurationManager, and i have the reference for it included.
My app config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="overwriteBackupFiles" value="False"/>
<add key="path" value="c:\temp"/>
</appSettings>
<ImageFormatsINeed>
<add key="type1" value="width=180&height=180"></add>
<add key="type2" value="width=220&height=220"></add>
<add key="type3" value="width=500&height=500"></add>
</ImageFormatsINeed>
</configuration>
and i am trying to acces those information like this:
string path = ConfigurationManager.AppSettings["path"];
var settings = ConfigurationManager.GetSection("ImageFormatsINeed");
When i didnt have the ImageFormatsINeed section i could get the path from AppSettings and it was working. But as soon as i added my ImageFormatsINeed section, everything stops working.
Now my question is how can i add custom sections in app.config that it will work, or should i just read my ImageInformation from some custom xml file or config file?
You have to use the tag <configSections> at the top in your app.config, for this case you should use the type AppSettingsSection
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ImageFormatsINeed" type="System.Configuration.AppSettingsSection" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="overwriteBackupFiles" value="False"/>
<add key="path" value="c:\temp"/>
</appSettings>
<ImageFormatsINeed>
<add key="type1" value="width=180&height=180"></add>
<add key="type2" value="width=220&height=220"></add>
<add key="type3" value="width=500&height=500"></add>
</ImageFormatsINeed>
</configuration>
Then in your C# code:
NameValueCollection settings_section = ConfigurationManager.GetSection("ImageFormatsINeed") as NameValueCollection;
Console.WriteLine(settings_section["type1"]);

Saving existing file: empty value attribute is removed

I have a config file like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="key1" value="value1" />
<add key="key2" value="" />
...
</appSettings>
...
</configuration>
I read the config, change the value of key1 and save the config
System.Configuration.Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Configuration.ConfigFile.Replace(".config", string.Empty));
appConfig.AppSettings.Settings["key1].Value = "newvalue1";
appConfig.Save(ConfigurationSaveMode.Minimal);
After this I get the following result:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="key1" value="newvalue1" />
<add key="key2"/>
...
</appSettings>
...
</configuration>
Why is the 'empty' value attribute removed for key "key2"?
When I try it with
appConfig.Save(ConfigurationSaveMode.Modified);
, the attribute is not removed. But I want to know why it's removed in the first case?
Thanks
<add key="key2" value="" />
and
<add key="key2" />
are both functionally equal. The value of key2 with or without that tag is a null string variable which is equivalent to the empty string.
Edit to reflect comment: Simply assign key2 the empty string in the same statement.
System.Configuration.Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Configuration.ConfigFile.Replace(".config", string.Empty));
appConfig.AppSettings.Settings["key1"].Value = "newvalue1";
appConfig.AppSettings.Settings["key2"].Value = "";
appConfig.Save(ConfigurationSaveMode.Minimal);
Second Edit for 2nd comment:
Well, you haven't provided enough information then. All of my answers answer your question. The two values from the first answer are functionally equivalent. Therefore, if you wish to see if the "key2" value = "" then you could simply run the following
if(appConfig.AppSettings.Settings["key2"] == null){
//If this hits, that means <add key="key2" value="" />
}
Please research how to ask a good question and adjust your question to reflect what you are really trying to ask.

How to retrieve the value for the Key in the Web.Config using DictionarySectionHandler

How to fetch the corresponding value of a key that I would get dynamically. I wish to use the system defined DictionarySectionHandler to do the job, of fetching the data from my custom built config section in the Web.config file
Code block in Web.Config
<section name="domainsource" type="System.Configuration.DictionarySectionHandler"/>
<domainSource>
<add key="0" value="170" />
<add key="1" value="171" />
<add key="2" value="172" />
<add key="3" value="173" />
<add key="12" value="174" />
</domainSource>
Sourcecode in the main cs file from where I wish to retrieve the data from the Web.Config
Hashtable statusCodes = ConfigurationManager.GetSection("domainSource") as Hashtable;
vDomainSource = statusCodes[vDomainID];
This is where I am stuck vDomainID would be a value 0/1/2/3/12, based on this value I need to fetch its respective Source from the Web.Config. Any help on this aspect would be really appreciated.
You have a missspelling in the defintion of the section domainsource -> domainSource. Further ensure that the elemnt is defined in an element. Then it should work.
<configuration>
<configSections>
<section name="domainSource" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<domainSource>
<add key="0" value="170" />
<add key="1" value="171" />
<add key="2" value="172" />
<add key="3" value="173" />
<add key="12" value="174" />
</domainSource>
</configuration>

How to use ConfigurationManager.AppSettings with a custom section?

I need to get "http://example.com" from using App.config file.
But at the moment I am using:
string peopleXMLPath = ConfigurationManager.AppSettings["server"];
I cannot get the value.
Could you point out what I am doing wrong?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<section name="device" type="System.Configuration.SingleTagSectionHandler" />
<section name="server" type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<device id="1" description="petras room" location="" mall="" />
<server url="http://example.com" />
</configuration>
I think you need to get the config section, and access that:
var section = ConfigurationManager.GetSection("server") as NameValueCollection;
var value = section["url"];
And you also need to update your config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<section name="device" type="System.Configuration.NameValueSectionHandler" />
<section name="server" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<device>
<add key="id" value="1" />
<add key="description" value="petras room" />
<add key="location" value="" />
<add key="mall" value="" />
</device>
<server>
<add key="url" value="http://example.com" />
</server>
</configuration>
Edit: As CodeCaster mentioned in his answer, SingleTagSectionHandler is for internal use only. I think NameValueSectionHandler is the preferred way to define config sections.
The SingleTagSectionHandler documentation says:
This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
You can retrieve it as a HashTable and access its entries using Configuration.GetSection():
Hashtable serverTag = (Hashtable)ConfigurationManager.GetSection("server");
string serverUrl = (string)serverTag["url"];
string peopleXMLPath = ConfigurationManager.AppSettings["server"];
gets the value from the appSettings part of the app.config file but you are storing your value in
<server url="http://example.com" />
Either put the value in the appSettings section as below or retrieve the value from its current location.
You need to add a key value pair to your config's appSettings section. As below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="server" value="http://example.com" />
</appSettings>
</configuration>
Your reading code is correct but you should probably check for null. If the code fails to read the config value the string variable will be null.
You're defining a configuration section instead of a value in AppSettings. You can simply add your setting to AppSettings:
<appSettings>
... may be some settings here already
<add key="server" value="http://example.com" />
</appSettings>
Custom config sections are typically used for more complicated configurations (e.g. multiple values per key, non-string values, etc.
If you want to get the value from the app settings your appsetting element in configuration file must have a key.
define your sever value as mentioned below under configuration section:
<configuration>
<appSettings>
<add key="server" value="http://example.com" />
</appSettings>
...
...
...
</configuration>
Now execute below code line to get the server url:
string peopleXMLPath = ConfigurationManager.AppSettings["server"].ToString();

Categories

Resources