0

I am asking about editing / deleting or adding sections to web.config I want to be able to add or delete connectionStrings section to my web.config file

here is my web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
   </system.webServer>
<connectionStrings />
</configuration>

I used the following code to add the section connectionStrings

 Dim doc As XmlDocument = New XmlDocument()
 Dim path As String = Server.MapPath("~/Web.Config")
 doc.Load(path)
 Dim newElem As XmlElement = doc.CreateElement("connectionStrings")
 doc.DocumentElement.AppendChild(newElem)
 doc.PreserveWhitespace = False
 Dim wrtr As XmlTextWriter = New XmlTextWriter(path, Encoding.Unicode)
 doc.WriteTo(wrtr)
 wrtr.Close()

I tried to manipulate the code to DELETE the section connectionStrings, but i was not able to do it. I used RemoveChild() function instead of the AppendChild() but i get errors like

Error 1 Value of type 'String' cannot be converted to 'System.Xml.XmlNode'. 

and

Object reference not set to an instance of an object.

can you help me with my code to delete the section connectionStrings ?

user3565664
  • 41
  • 1
  • 1
  • 5
  • 1
    possible duplicate of [Removing nodes from an XmlDocument](http://stackoverflow.com/questions/20611/removing-nodes-from-an-xmldocument) – mason Apr 23 '14 at 19:25
  • Regardless of your problem, this is a bad idea. When an IIS application's `Web.config` file is edited, the IIS `AppPool` will recycle. This will cause the thread actually editing the configuration file to terminate and thus terminate the HTTP request, giving a bad UX. You should not edit the `Web.config` file of a web application from the application itself. There's also the possible security implications of allowing users of the website to edit its configuration. – Martin Costello Apr 23 '14 at 19:34
  • @mason i am sorry sir, i am very novice and i can not use the "duplicate" question you mentioned as a guide. would you please help me with my code to remove the section? – user3565664 Apr 23 '14 at 19:42
  • @mason "connectionstrings" – user3565664 Apr 23 '14 at 19:45
  • @mason can you post it in C#? sorry for bothering sir. – user3565664 Apr 23 '14 at 20:12

3 Answers3

0

To delete the connectionstrings node, go to the parent node (in this case configuration) and then use the RemoveChild() function. This is the opposite of the AppendChild() function. You'll need to pass in the element you want to delete.

mason
  • 31,774
  • 10
  • 77
  • 121
0
Dim doc As XmlDocument = New XmlDocument()
Dim path As String = Server.MapPath("~/Web.Config")
doc.Load(path)
Dim connNode As XmlNode = doc.SelectSingleNode("//connectionStrings")
Dim myparent As XmlNode = connNode.ParentNode
myparent.RemoveChild(connNode)
doc.PreserveWhitespace = False
Dim wrtr As XmlTextWriter = New XmlTextWriter(path,Encoding.Unicode)
doc.WriteTo(wrtr)
wrtr.Close()
Mohamed Kamal
  • 2,377
  • 5
  • 32
  • 47
0

You an use 'WebConfigurationManager' class directly to delete the specific key from web.config. see below code snippet to remove 'connection' string

Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
config.AppSettings.Settings.Remove("connectionstring");
config.Save();
koolprasad2003
  • 299
  • 3
  • 23