Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am having a appsetting file as below.
HTML
<appsettings>
<add key="test1" value="chk1" />
<add key="test2" value="chk2" />
<add key="test3" value="chk3" />
</appsettings>


but the configuration section is missing in the appsetting file as it has been refered from the web.config file.

I want to read the above appsetting file as xml and add a <configuration> arround the appsetting node to read the keys from an external application.

After reading i want to remove that configuraion node
can any body help me out to add and remove the configuration node arround appsetting node ?
Posted
Updated 18-May-15 6:39am
v2
Comments
Member 10865846 18-May-15 12:03pm    
<appsettings>
<add key="DSPRegionsFieldName" value="DSPRegions">
<add key="DSPOfferingsFieldName" value="DSPOfferings">
<add key="DSPProcessorsFieldName" value="DSPProcessors">
Member 10865846 18-May-15 12:06pm    
i just want to add configuraion node arround the appsetting node.pls help?
Andy Lanng 18-May-15 12:24pm    
As far as I know, you cannot change the appsettings in the config /file/ but you can load an alternative configuration. I think you can write one at runtime and use that instead.
The ConfigurationManager is readonly at runtime.
Abhipal Singh 18-May-15 14:09pm    
Without configuration node, this xml will not be treated as a config file by ConfigurationManager. You can just read it in an XmlDocument (Load(string filepath)) and modify it the way you want (and then save() it).

However, Can you please let us know, what are you trying to achieve by doing this. May be the we can figure out a more elegant solution.

You can add and remove sections from a valid configuration file by following the link below:
http://yizeng.me/2013/08/31/update-appsettings-and-custom-configuration-sections-in-appconfig-at-runtime/
virusstorm 18-May-15 15:00pm    
Is this a .net configuration file or your own, custom configuration file?

1 solution

Please try below code for your requirement...

XDocument xmlDoc = XDocument.Load(filePath);
bool isExists = (from data in xmlDoc.Element("appsettings")
				select data).Any();
if (!isExists)
{
	    XmlElement parent = xmlDoc.CreateElement("configuration");
            //Now find that appSettings node and append it to parent Element
}

//Delete Node
XmlNodeList newXMLNodes = xmlDoc.SelectNodes("/configuration/appsettings");
foreach (XmlNode child in newXMLNodes)
{
  child.ParentNode.RemoveAll();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900