I have the following method which is basically supposed to ADD a new key-value pair into my web.config file each time it's called but instead it's UPDATE the existent values at position [0] [1]:
static public void InsertKeyValuePair(string key, string value)
{
XmlDocument doc = new XmlDocument();
doc.Load("Web.config");
// This should find the appSettings node (should be only one):
XmlNode nodeAppSettings = doc.SelectSingleNode("//appSettings");
// Create new <add> node
XmlNode nodeNewKey = doc.CreateElement("add");
// Create new attribute for key=""
XmlAttribute attributeKey = doc.CreateAttribute("key");
// Create new attribute for value=""
XmlAttribute attributeValue = doc.CreateAttribute("value");
// Assign values to both - the key and the value attributes:
attributeKey.value = "myKey";
attributeValue.value = "myValue";
// Add both attributes to the newly created node:
nodeNewKey.Attributes.Append(attributeKey);
nodeNewKey.Attributes.Append(attributeValue);
// Add the node under the
nodeAppSettings.ChildNodes.Add(nodeNewKey);
doc.Save("Web.config");
}
I think there was a Configuration manager class to handle this. Not quite sure. Check the System.Configuration and System.Web.Configuration namespaces.
If you really need to add a key using XmlDocument you can do it this way:
static public void InsertKeyValuePair(string key, string value)
{
XmlDocument doc = new XmlDocument();
doc.Load("Web.config");
// This should find the appSettings node (should be only one):
XmlNode keyAge = doc.SelectSingleNode("//appSettings/add[@key='age']");
keyAge.Attributes["value"].Value = "25";
doc.Save("Web.config");
}
YairNevet
Member
96 Points
181 Posts
Adding new key into web.config file using c#
Dec 08, 2008 03:01 PM|LINK
Hi all,
I have the following method which is basically supposed to ADD a new key-value pair into my web.config file each time it's called but instead it's UPDATE the existent values at position [0] [1]:
static public void InsertKeyValuePair(string key, string value) { XmlDocument doc = new XmlDocument(); doc.Load("Web.config"); XmlNodeList nodeList = doc.GetElementsByTagName("appSettings"); XmlNodeList nodeAppSettings = nodeList[0].ChildNodes; XmlAttributeCollection xmlAttCollection = nodeAppSettings[0].Attributes; xmlAttCollection[0].InnerXml = key; xmlAttCollection[1].InnerXml = value; doc.Save("Web.config"); }Microsoft MCAD.NET
donchevp
Participant
1216 Points
228 Posts
Re: Adding new key into web.config file using c#
Dec 08, 2008 03:22 PM|LINK
static public void InsertKeyValuePair(string key, string value) { XmlDocument doc = new XmlDocument(); doc.Load("Web.config"); // This should find the appSettings node (should be only one): XmlNode nodeAppSettings = doc.SelectSingleNode("//appSettings"); // Create new <add> node XmlNode nodeNewKey = doc.CreateElement("add"); // Create new attribute for key="" XmlAttribute attributeKey = doc.CreateAttribute("key"); // Create new attribute for value="" XmlAttribute attributeValue = doc.CreateAttribute("value"); // Assign values to both - the key and the value attributes: attributeKey.value = "myKey"; attributeValue.value = "myValue"; // Add both attributes to the newly created node: nodeNewKey.Attributes.Append(attributeKey); nodeNewKey.Attributes.Append(attributeValue); // Add the node under the nodeAppSettings.ChildNodes.Add(nodeNewKey); doc.Save("Web.config"); }I think there was a Configuration manager class to handle this. Not quite sure. Check the System.Configuration and System.Web.Configuration namespaces.If you really need to add a key using XmlDocument you can do it this way:
My Blog
YairNevet
Member
96 Points
181 Posts
Re: Adding new key into web.config file using c#
Dec 08, 2008 04:12 PM|LINK
Thank you Calibre!
Microsoft MCAD.NET
donchevp
Participant
1216 Points
228 Posts
Re: Adding new key into web.config file using c#
Dec 09, 2008 06:46 AM|LINK
Can you please mark so other guys know what solved your problem?
Thanks,
Pavel
My Blog
YairNevet
Member
96 Points
181 Posts
Re: Adding new key into web.config file using c#
Dec 09, 2008 07:58 AM|LINK
Hi Pavel,
Before i'm approving your message as helped one, can you please tell me how can I replace a node in the AppSettings section?
For instance, if I need to replace: <add key="Age" value="24"> in <add key="Age" value="25">
How?
Microsoft MCAD.NET
donchevp
Participant
1216 Points
228 Posts
Re: Adding new key into web.config file using c#
Dec 09, 2008 01:21 PM|LINK
Sure.
Something like this should work:
static public void InsertKeyValuePair(string key, string value) { XmlDocument doc = new XmlDocument(); doc.Load("Web.config"); // This should find the appSettings node (should be only one): XmlNode keyAge = doc.SelectSingleNode("//appSettings/add[@key='age']"); keyAge.Attributes["value"].Value = "25"; doc.Save("Web.config"); }My Blog
donchevp
Participant
1216 Points
228 Posts
Re: Adding new key into web.config file using c#
Dec 09, 2008 01:22 PM|LINK
Unfortunatelly the web editor recognizes the // in the xpath as a comment [^o)]
My Blog
YairNevet
Member
96 Points
181 Posts
Re: Adding new key into web.config file using c#
Dec 09, 2008 02:00 PM|LINK
Thank you Pavel!
You really helped me!
Yair
Microsoft MCAD.NET
donchevp
Participant
1216 Points
228 Posts
Re: Adding new key into web.config file using c#
Dec 09, 2008 02:40 PM|LINK
Glad to hear that.
My Blog