How to read values like culture etc. from the app.config file below.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="LibrarySetting" type="LibraryConfigUtilities.ConfigurationSectionHandler, LibraryConfigUtilities" />
</configSections>
<LibrarySetting>
<Country Culture="tr-TR" Currency="TRY" DailyPenaltyFee="5,25" PenaltyAppliesAfter="3">
<WeekendSetting>
<Weekend Day="6"/>
<Weekend Day="0"/>
</WeekendSetting>
<HolidaySetting>
<Holiday Date="25.11.2009"/>
<Holiday Date="26.11.2009"/>
<Holiday Date="27.11.2009"/>
</HolidaySetting>
</Country>
<Country Culture="ar-AE" Currency="AED" DailyPenaltyFee="8.00" PenaltyAppliesAfter="4">
<WeekendSetting>
<Weekend Day="5"/>
<Weekend Day="6"/>
</WeekendSetting>
<HolidaySetting>
<Holiday Date="25.11.2009"/>
<Holiday Date="26.11.2009"/>
<Holiday Date="27.11.2009"/>
</HolidaySetting>
</Country>
</LibrarySetting>
</configuration>
I have below code in my program. And i want to read values from the above app.config.
private List<Country> settingList = new LibrarySetting().LibrarySettingList;
and i added
using LibraryConfigUtilities;
What about using ConfigurationManager.GetSection("LibrarySetting");
See if this will help get you anywhere.
Use this link as a reference, has some good help. App Config Group Help
Related
I have an App.config of the form
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Custom.Config" type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<kCura.Config configSource="Custom.config" />
</configuration>
With a custom configuration file Custom.config of the form
<?xml version="1.0" encoding="utf-8"?>
<Custom.Config>
<add key="foo" value="bar" />
</Custom.Config>
I've inherited this code, and there is no existing ConfigurationSection or ConfigurationSectionHandler associated with this Custom.Config section.
I can access the value of foo just fine with
DirectCast(System.Configuration.ConfigurationManager.GetSection("Custom.Config"), System.Collections.IDictionary)
But now I would like to update this value, programatically (for testing purposes). Is this possible?
I've read the following, and I'm still stumped; they seem to imply that this namespace is only for reading values, not full CRUD:
How to: Create Custom Configuration Sections Using ConfigurationSection
How to: Create Custom Configuration Sections Using IConfigurationSectionHandler
You'll have to modify the Custom.config file on disk by using XDocument then you'll need to call RefreshSection on the ConfigurationManager
This will then refresh the named section so the next time that it is retrieved it will be re-read from disk.
Im having a hard time remembering how to do this, and most of the examples I'm seeing dont really cover my issue. I'm trying to read in the below XML file, so that if a user selects a Tool Type from a drop-down menu, the variables for said tool will populate a form on screen. I just have no clue how to collect all the elements/attributes for a specific tool.
<?xml version="1.0" encoding="UTF-8"?>
<Tool_menu>
<tool name="A">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>Confg1</name>
</configuration>
<configuration>
<name>Confg2</name>
</configuration>
<configuration>
<name>Confg3</name>
</configuration>
<configuration>
<name>Confg4</name>
</configuration>
</configuration_list>
</tool>
<tool name="B">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>n/a</name>
</configuration>
<configuration>
<name>n/a</name>
</configuration>
</configuration_list>
</tool>
<tool name="C">
<emails>
<email severity="Low">
<address>reg#test.com</address>
</email>
<email severity="High">
<address>notReg#test.com</address>
</email>
</emails>
<configuration_list>
<configuration>
<name>200Scope</name>
</configuration>
<configuration>
<name>300Scope</name>
</configuration>
<configuration>
<name>600Scope</name>
</configuration>
<configuration>
<name>900Scope</name>
</configuration>
</configuration_list>
</tool>
</Tool_menu>
what I'd want is for a user to select 'tool C' and see a list of configurations available on tool C, name of the tool, and a dropdown of options for who to email (low/high severity) that would be tool specific
**using .net 4.5
Access nodes using XPath.
Take a look at some tutorials here or here
In your case, accessing tools C can be achieved like this:
XmlDocument doc = new XmlDocument();
doc.Load(#"c:\temp\tools.xml");
var toolCemails = doc.SelectNodes("//tool[#name='C']/emails/email"); //two nodes with email tag
var toolCconfigs = doc.SelectNodes("//tool[#name='C']/configuration_list/configuration"); //four config nodes
You could take a look at this post on SO: LINQ to read XML
edit: also, in the comments below your question, #zx485 posted a link to another helpful SO post, here: Bind XML data to a Dropdownlist c#
That might help get you started. If not, please try to narrow your question to something more specific - preferably with some code that we can help you with.
As your question is written right now, it seems that you are asking us to do this for you rather than help you with a specific question.
I don't know why but i don't have AppSettings in my app.config.file.
If i add it, when i run my apps, appSetting is deleted, so i'm unable to read my data !
Here is my file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</configuration>
And in c# :
string RepertoireEntree = ConfigurationManager.AppSettings["RepertoireEntree"];
You have your app settings in a appSettings element, else it won't be recognized:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>
</configuration>
By the way, it is easier to use settings the usual way. You can do this by creating a settings file from your Project Settings > Settings tab.
Visual Studio will generate the XML elements in the app.config for you and you can reference your property like this:
string re = Properties.Settings.Default.RepertoireEntree;
You do not have your xml correct in the app.config file.
note the start and end tags for appSettings
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="RepertoireEntree" value="E:\DEV\Autres\DevAgréga\Input\*.txt"/>
<add key="RepertoireSortie" value="E:\DEV\Autres\DevAgréga\Output\"/>
</appSettings>
</configuration>
PS
While this is probably not an issue for you (but moreso for future readers)
Make sure you have this reference:
Namespace: System.Configuration
Assembly: System.Configuration (in System.Configuration.dll)
as noted on MSDN
I have a simple config section as follows
<configuration>
<configSections>
<section name="Test" type="System.Configuration.DictionarySectionHandler"/>
<section name="Test1" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<Test>
<add key="foo" value="1"/>
</Test>
<Test1>
<add key="bar" value="20"/>
</Test1>
</configuration>
And I am accessing it from code as
var blah = ConfigurationManager.GetSection("Test");
But I always get null. I tried everything but could not figure out whats going on. Could someone please help me with this?
Thanks,
I have an app config in a c# project I need to edit at run time. As part of this I have a custom section collection I need to select a single node for. This is the xml and selection statement from my immediate window:
node = xmlDoc.SelectSingleNode("//Customers/add[#id='1']");
null
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="customers" type ="ImporterSupport.CustConfigSection, ImporterSupport"/>
</configSections>
<customers>
<add id ="1" license="gh620g0g0g0g0g3p" ServerAddress ="localhost" ServerPort="8292" SettingsFile ="AutoImportTest.txt" Confirm ="false" DisableRemoveRecips="true" DisableRecoverRecips="true" DisableAlphaId ="true" />
</customers>
</configuration>
Xml is case sensitive. Use
var node = xmlDoc.SelectSingleNode("//customers/add[#id='1']");