I have some problem with getting config info, how to get isUpdatable value for some value test1 or test2, or test3 if paramiter will get outside.
I have this sample in config.
<configSections>
<sectionGroup name ="UpdateSettings">
<section name="test1" type="System.Configuration.NameValueSectionHandler"/>
<section name="test2" type="System.Configuration.NameValueSectionHandler"/>
<section name="test3" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<UpdateSettings>
<test1>
<add key="isUpdatable" value="0"/> <!-- get from service - 1, get from config - 0,-->
</test1>
<test2>
<add key="isUpdatable" value="1"/>
</test2>
<test3>
<add key="isUpdatable" value="1"/>
</test3>
</UpdateSettings>
And this peace of C# code
name = "test1";
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup UpdateSettings = config.GetSectionGroup("UpdateSettings");
/* this is Connect beetwen selection group and selection, but how?*/
NameValueCollection sectionSettings = ConfigurationManager.GetSection(name) as NameValueCollection;
var value = sectionSettings["isUpdatable"];
You were very close, you have array of values so sectionSettings[0] will get you first one and sectionSettings[1] second one and so on.
name = "test1";
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup UpdateSettings = config.GetSectionGroup("UpdateSettings");
NameValueCollection sectionSettings = ConfigurationManager.GetSection(name) as NameValueCollection;
var value = sectionSettings[0]["isUpdatable"];
Related
I want to select xml node based on attribute. I'm very new to how linq to xml works, and can't write proper query. How can I fix it?
My XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Enable0" value="true" />
<!-- dumb comment -->
<add key="Enable1" value="false" />
<!-- dumb comment1-->
<add key="Enable2" value="true" />
<add key="Enable3" value="false" />
<!-- dumb comment2 -->
<add key="Enable4" value="true" />
</appSettings>
<asdf>
<a key="b"></a>
<a key="c"></a>
<a key="d"></a>
</asdf>
</configuration>
My attempt:
private string GetAttribute(string name)
{
//???
var query = from node in deafultElement.Elements("add")
where node.Attribute("key").Value == name
select node.Attribute("value").value;
return query.toString();
//currently returns "System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]"
}
You can also use linq methods' syntax to reach the same result.
string value = defaultElement.Elements("add")
.FirstOrDefault(n => n.Attribute("key").Value == name)
.Attribute("value").Value;
var query = (from node in deafultElement.Elements("add")
where node.Attribute("key").Value == name
select node.Attribute("value").Value).FirstOrDefault();
return query
I have an XML like:
<configuration>
<connectionStrings>
<add name="name1" value="value1">
<add name="name2" value="value2">
</connectionStrings>
<configuration>
In this i get
XmlNodeList nodeList = doc.GetElementsByTagName("connectionStrings");
foreach(XmlNode xn in nodeList)
{
var anode = xn.SelectSingleNode("add");
}
Here "var anode" is returning me right values of "name" attribute.
But when my xml is like:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<connectionStrings>
<add name="name1" value="value1">
<add name="name2" value="value2">
</connectionStrings>
<configuration>
I get anode as null.
Please help.
The following code works for me:
var xElement = XElement.Load("Sample.xml");
var elements = xElement.Elements().Elements();
foreach (var element in elements)
{
var name = element.Attribute("value").Value;
}
Please try this.
Note: The Sample.xml contains:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<connectionStrings>
<add name="name1" value="value1"/>
<add name="name2" value="value2"/>
</connectionStrings>
</configuration>
The xml file which you posted didnot close properly. Above xml is properly formatted.
i have the following app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<xx>
<add key="x" value="1.1.1.1" />
<add key="y" value="1.1.1.1" />
<add key="z" value="1.1.1.1" />
<add key="w" value="6" />
</xx>
<yy>
<add key="Wireshark" value="1" />
</yy>
<zz>
<add key="Firmware1" value="C:\Users\Desktop\Download.txt/>
<add key="Firmware2" value="C:\Users\Desktop\Download.txt" />
</zz>
</configuration>
how can i have an array for x, y and w. should i need appsettings ? does this xml is valid?
First, you need to write a custom class for each custom section in the configuration file; another option is to use one of the built-in types.
For example;
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Default" type="System.Configuration.NameValueSectionHandler" />
<section name="Maestro" type="System.Configuration.NameValueSectionHandler" />
<section name="Drive" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<Default>
<add key="gmasIP" value="192.168.2.3" />
<add key="hostIP" value="192.168.2.2" />
<add key="GatewayIP" value="192.168.2.4" />
<add key="relayCOM" value="6" />
</Default>
<Maestro>
<add key="Wireshark" value="1" />
</Maestro>
<Drive>
<add key="FirmwarePath" value="C:\Users\rinat\Desktop\Download.txt/>
<add key="FirmwarePalPath" value="C:\Users\rinat\Desktop\Download.txt" />
</Drive>
</configuration>
If you want to get the values as an array:
var defaultItems= ConfigurationManager.GetSection("Default") as NameValueCollection;
List<string> temp = new List<string>();
if (defaultItems!= null)
{
foreach (var key in defaultItems.AllKeys)
{
string val= defaultItems.GetValues(key).FirstOrDefault();
temp.Add(val);
}
}
string[] arr = temp.ToArray();
This is the simple snippet taking descendants values from XML,
string[] arr1 = XDocument.Load(#"C:\xxx.xml").Descendants("Default")
.Select(element => element.Value).ToArray();
string[] arr2 = XDocument.Load(#"C:\xxx.xml").Descendants("Maestro")
.Select(element => element.Value).ToArray();
string[] arr3 = XDocument.Load(#"C:\xxx.xml").Descendants("Drive")
.Select(element => element.Value).ToArray();
use this code,.
You could read custom section of config files as below,
var defaultSettings = ConfigurationManager.GetSection("Default") as NameValueCollection; //You can replace Default with any other node name like Maestro, Drive
string hostIp = defaultSettings["hostIP"].ToString(); //you can access any of the key value under Default node in your xml config now.
Note that you may have to add a reference to System.Configuration from Framework.
I have created custom tags in web.config file.These values am storing in a hash table.After storing in the hash table am reading both key and value am able to read the key but not the value.Here is my code in web.config
<configSections>
<sectionGroup name="BlockIpSection">
<section name="ipslist" type="CustomConfigurationHandler.CustomConfiguration, CustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/>
</sectionGroup>
</configSections>
<BlockIpSection>
<ipslist>
<ipaddress1 value="100"></ipaddress1>
<ipaddress2></ipaddress2>
</ipslist>
</BlockIpSection>
Here there are 2 tags section group name and section name.I have created the tags as ipaddress1 and ipaddress2.Am storing value 100 to ipaddress1 tag.
I am reading this web.config like this
Hashtable config = (Hashtable)ConfigurationManager.GetSection("BlockIpSection/ipslist");
foreach (string key in config.Keys)
{
Response.Write(key + "<br>");
}
Tags from web.config we are storing in hashtable and reading using key.when i use config[key] to read the value its giving error.
Try to use
App.BlockIpSection
or
ConfigurationManager.AppSettings["BlockIpSection"]
Define your keys and values in web. config. like this .
<BlockIpSection>
<ipslist>
<add key="ipaddress1 " value="100"/>
<add key="ipaddress2 " value="100"/>
// IP keys and values ....
</ipslist>
</BlockIpSection>
Hashtable config = (Hashtable)ConfigurationManager.GetSection("BlockIpSection/ipslist");
foreach (DictionaryEntry dicEntry in config)
//OR
// foreach (KeyValuePair<string,string> dicEntry in config)
{
string key = Convert.ToString(dicEntry.Key);
string val = Convert.ToString(dicEntry.Value);
// Your code goes here
}
first of all, you need to mention key and value for both tags in web.config and then
Use NameValueCollection instead of Hashtable.
problem with Hashtable is given below:
try this Block of code:
NameValueCollection nvObj = new NameValueCollection();
nvObj = ConfigurationManager.GetSection("MyCustomSettings") as NameValueCollection;
Response.Write(hs["key1"] + "</br>");
I have a simple program that would let the user add a section to a custom config file, it would have more settings than what is shown. I populate a datagridview with a list of all configurations. My problem is, the method to populate the listbox wouldn't know all the names of the sections that the user may have added, I'm trying to be dynamic. Is there an easy way to loop through these sections and get their names? Or do I have to make a Section, Collection, and Elements in order to to this?
Thanks.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Jason" type="SQLQueryOutput.SQLQueryOutputConfigSection, SQLQueryOutput, Version=1.0.0.0, Culture=neutral, PublicKeyToken=760d257b40400289" />
<section name="Steve" type="SQLQueryOutput.SQLQueryOutputConfigSection, SQLQueryOutput, Version=1.0.0.0, Culture=neutral, PublicKeyToken=760d257b40400289" />
</configSections>
<Jason OutputFilePath="C:\temp\jason.txt" />
<Steve OutputFilePath="C:\temp\steve.txt" />
</configuration>
How about using Linq To Xml to parse your config file. For example,
var xDoc = XDocument.Load(configFile);
var sections = xDoc.XPathSelectElements("//configSections/section")
.Select(x=>x.Attributes().ToDictionary(a=>a.Name,a=>a.Value))
.ToList();
var name = sections[0]["name"];
or
var outputFilePaths = xDoc.Root.Elements()
.Where(d => d.Name.LocalName != "configSections")
.ToDictionary(e => e.Name.LocalName, e => e.Attribute("OutputFilePath").Value);
Actually your configSections element can contain sectionGroup elements also. With Linq to Xml:
XDocument xdoc = XDocument.Load(config_file_path);
var names = xdoc.Root.Element("configSections")
.Descendants("section") // selects also sectionGroup/section
.Select(s => (string)s.Attribute("name"));