Following config file causes error because of & in 'url' value:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="url" value ="http://www.example.com/?user=admin&password=1234"/>
</appSettings>
</configuration>
The question is which encoding to use for key/value in a config file ? (i.e Url Encoding ...)
This is an XML file. As such, you must XML-escape all reserved characters:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="url" value ="http://www.example.com/?user=admin&password=1234"/>
</appSettings>
</configuration>
Programmatically you can perform this type of escaping using the System.Web.HttpUtility.HtmlEncode method. Yes, its name suggests HTML specific encoding, but in my experience this is equivalent to XML escaping.
Related
Following config file causes error because of & in 'url' value:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="url" value ="http://www.example.com/?user=admin&password=1234"/>
</appSettings>
</configuration>
The question is which encoding to use for key/value in a config file ? (i.e Url Encoding ...)
This is an XML file. As such, you must XML-escape all reserved characters:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="url" value ="http://www.example.com/?user=admin&password=1234"/>
</appSettings>
</configuration>
Programmatically you can perform this type of escaping using the System.Web.HttpUtility.HtmlEncode method. Yes, its name suggests HTML specific encoding, but in my experience this is equivalent to XML escaping.
I'm new to C# and am still in the process of figuring out the best practices for certain things.
I want to know where I could store settings such as paths (eg. For file uploads) and other assorted variables so that they can be accessed anywhere in the project.
Should I just create a class with static variables or is there a better approach to storing these settings?
You'd better save this in the web.config since this can be changed after compilation.
The appSettings element is reserved for this kind of functionality. You can even split this part off in a different file so it is totally clear this in your specific config.
Example web.config only:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DocumentDirectory" value="~/Documents" />
</appSettings>
</configuration>
Or:
web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="appSettings.xml" />
</configuration>
And a separate appSettings.xml:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="DocumentDirectory" value="~/Documents" />
</appSettings>
You can read those settings like this:
using System.Configuration;
using System.Web.Configuration;
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
if (config.AppSettings.Settings.Count > 0)
{
KeyValueConfigurationElement customSetting = config.AppSettings.Settings["DocumentDirectory"];
if (customSetting != null)
{
string directory = customSetting.Value;
}
}
When I execute my app (Winform)from the computer where it was developed there is no error, but When I execute this in another computer I get the error. My App.config is like this:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="SecurityKey"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0"sku="...."/>
</startup>
</configuration>
and this is the line that I use:
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
I already tried to follow this The key 'UserID' does not exist in the appSettings configuration section, but it is still the same.
Any suggestions?
the appSettings in the .config file is different from .settings file.
Take a look at ConfigurationManager.AppSettings Property.
I'd also mention that I have no idea how either the settingsReader nor the ConfigurationManager work with a key with no value:
<add key="SecurityKey"/> <!-- no value? -->
<add key="SecurityKeyWithValue" value="myvalue"/>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SecurityKey" value="Syed Moshiur Murshed"/>
</appSettings>
</configuration>
I am trying to print to Console.Write the value of the key name from the following app.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="name" value="Chan" />
</appSettings>
</configuration>
C# code :
Console.Write(ConfigurationManager.AppSettings["name"]);
Nothing gets printed in the console. Why is this ?
Note: I have added a reference to the System.Configuration dll
below code gives you the content of active config file.
var content = File.ReadAllLines(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
Check what you get as content, is it contain key="name" value="Chan" or something else
?
if you given <add key="name" value="Chan" /> then
ConfigurationManager.AppSettings["name"] should return as Chan
Given that your XML file (app.config) is properly formatted, try below.
Declare a variable and assign the variable the AppSettings value. Similar like-
string sName = "";
sName = ConfigurationManager.AppSettings["name"].ToString();
I've used the Configuration Section Designer (csd) to generate an xml configuration file to my application.
Now, I want to work with this xml file (on c#) and do 2 things:
1. read a specific value (searching by its field), something like
txtbox_username.Text = ConfigurationManager.AppSettings["userName"];
2. write a specific value, something like
config.AppSettings.Settings["userName"].Value = txtbox_username.Text;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("configuration");
p.s: this is how i perform read/write for regular xml files that looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
add key="userName" value="Value1"
</appSettings>
</configuration>
but the csd generated xml looks different...
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="sectionName" type="Sample.ConfigurationSection.sectionName, Sample.ConfigurationSection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</configSections>
<sectionName xmlns="Sample.ConfigurationSection">
<logFile debugLevel="3" filePath="c:\new" maxFileCount="10" maxFileLength="1000"/>
<results path="C:\Results"/>
<details user="blabla" pass="12345" code="abc"/>
<stuff fromAddress="from#gmail.com" toAddress="to#gmail.com" sendMail="true""/>
</sectionName>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
here, I want to edit and save, for example, user/pass/code fields.
In short (and using these extensions: http://searisen.com/xmllib/extensions.wiki) you can do this to read/write the user,
XElement file = XElement.Load(xmlFile);
XNamespace ns = "Sample.ConfigurationSection";
XElement section = file.GetElement(ns + "sectionName");
string user = section.Get("details/user", string.Empty);
section.Set("details/user", "bubba", true); // true = set as attribute
file.Save(xmlFile);