locked
Updating web.config RRS feed

  • Question

  • Actually right now I have to update the web.config file which have the settings of my web service project.

    I have some tags which already exist there…

    I want to replace this full tag with new values….

    In this project, I am able to get the string (actually xml tag sets) from the web.config file.

    My task is to generate a new string (xml tag string) and replace the old one with the new one.

    For more clarification, let me put some exmaple:

    i have web.config like:

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

           <configSections>
                 <sectionGroup name="Type2">
                         <section name="Field1" type="MultiConfigSections.MultConfigurationSectionHandler2, MultiConfigSections"/>
                  </sectionGroup>

           </configSections>

      <myCustomTag>
          <childTag>
                 <!--          some more tag in child tags or some text      --!>
           </childTag>
      </myCustomTag>


      <httpHandlers>
        <new data="boss">
          hello boss
        </new>
        <add key =" next " value =" new"/>
      </httpHandlers>
           <appSettings>

                  <add key="Field1" value="c:\File1.txt" />

           </appSettings>
    </configuration>



    Here, for <myCustomTag>, i am able to find the string inside this customTag. Like:

    String str1 = "<childTag> <!--          some more tag in child tags or some text      --!> </childTag>"

    here str1 is my string...

    for a new string "str2", like:

    String str2 = "<newChildTag> <!--          some new tags of text in child tags   --!> </newChildTag>"

    I want to replace str1 from str2 in my web.config.....

    and hence the resulting web.config will be:


    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

           <configSections>
                 <sectionGroup name="Type2">
                         <section name="Field1" type="MultiConfigSections.MultConfigurationSectionHandler2, MultiConfigSections"/>
                  </sectionGroup>

           </configSections>

       <myCustomTag>
          <newChildTag>
                    <!--          some new tags of text in child tags   --!>
          </newChildTag>
      </myCustomTag>

      <httpHandlers>
        <new data="boss">
          hello boss
        </new>
        <add key =" next " value =" new"/>
      </httpHandlers>
           <appSettings>

                  <add key="Field1" value="c:\File1.txt" />

           </appSettings>
    </configuration>




    I have made the change in web.config as bold so that my task is clear...

    Please help me for the same....     

    hima
    • Moved by Peter Ritchie Monday, June 2, 2008 7:32 PM more appropriate forum
    Friday, May 30, 2008 7:25 AM

Answers

  • In case you're going to change your xml format, a ConfigurationSectionHandler would be rather useless... In that case you could use XmlDocument and friends  in System.Xml, eg:

                XmlDocument xmlDocument = new XmlDocument(); 
                xmlDocument.Load(@"input.xml"); 
                XmlNode myCustomTagNode = xmlDocument.SelectSingleNode("//configuration/myCustomTag/childTag"); 
                myCustomTagNode.InnerXml = "<newChildTag><!-- some new tags of text in child tags !--></newChildTag>"
                xmlDocument.Save(@"output.xml"); 


    Tim Van Wassenhove - Please remember to mark the replies as answers if they help.
    Saturday, May 31, 2008 9:44 AM
  • If you want edit runtime webconfig you must use WebConfigurationManager :

    you use System.Web.Configuration

    using System.Web.Configuration; 

    Create Configuration

    Configuration config;  
    config = WebConfigurationManager.OpenWebConfiguration("~"); 

    if you dont insert webConfig, method load Root WebConfig

    you can use this class for edit webconfig
    - AuthenticationSection
    - AuthorizationSection
    - CacheSection
    - CustomErrorsSection
    - HealthMonitoringSection
    - HttpHandlersSection
    - HttpModulesSection
    - MembershipSection
    - OutputCacheSection
    - PagesSection
    - TrustSection
    - UrlMappingsSection
    - Youy Custom Section ...

    Example :
    Add new Profile

    ProfileSection compilation;     
    compilation = config.GetSection("system.web/profile"as ProfileSection;     
        
    ProfilePropertySettings nome = new ProfilePropertySettings("myProperty");     
    nome.Type = "System.String";     
    nome.SerializeAs = SerializationMode.Binary;     
        
    compilation.PropertySettings.Add(nome);     
        
    config.Save();    
     

    Developer
    Monday, June 2, 2008 4:00 PM
  • This was moved out of the forum.

    You can ask this in another MSDN forum, or I can move it into a specific forum if you'd like.

    Thanks!


    Ed Price (a.k.a User Ed), SQL Server Experience Program Manager (Blog, Twitter, Wiki)

    Monday, February 20, 2012 6:24 AM

All replies

  • Hi,

    It would be better to use WebConfigurationManager class for this purpose and create custom ConfigurationSection for myCustomTag xml section.

    Vitaliy Liptchinsky
    Friday, May 30, 2008 8:00 AM
  • In case you're going to change your xml format, a ConfigurationSectionHandler would be rather useless... In that case you could use XmlDocument and friends  in System.Xml, eg:

                XmlDocument xmlDocument = new XmlDocument(); 
                xmlDocument.Load(@"input.xml"); 
                XmlNode myCustomTagNode = xmlDocument.SelectSingleNode("//configuration/myCustomTag/childTag"); 
                myCustomTagNode.InnerXml = "<newChildTag><!-- some new tags of text in child tags !--></newChildTag>"
                xmlDocument.Save(@"output.xml"); 


    Tim Van Wassenhove - Please remember to mark the replies as answers if they help.
    Saturday, May 31, 2008 9:44 AM
  • If you want edit runtime webconfig you must use WebConfigurationManager :

    you use System.Web.Configuration

    using System.Web.Configuration; 

    Create Configuration

    Configuration config;  
    config = WebConfigurationManager.OpenWebConfiguration("~"); 

    if you dont insert webConfig, method load Root WebConfig

    you can use this class for edit webconfig
    - AuthenticationSection
    - AuthorizationSection
    - CacheSection
    - CustomErrorsSection
    - HealthMonitoringSection
    - HttpHandlersSection
    - HttpModulesSection
    - MembershipSection
    - OutputCacheSection
    - PagesSection
    - TrustSection
    - UrlMappingsSection
    - Youy Custom Section ...

    Example :
    Add new Profile

    ProfileSection compilation;     
    compilation = config.GetSection("system.web/profile"as ProfileSection;     
        
    ProfilePropertySettings nome = new ProfilePropertySettings("myProperty");     
    nome.Type = "System.String";     
    nome.SerializeAs = SerializationMode.Binary;     
        
    compilation.PropertySettings.Add(nome);     
        
    config.Save();    
     

    Developer
    Monday, June 2, 2008 4:00 PM
  • This was moved out of the forum.

    You can ask this in another MSDN forum, or I can move it into a specific forum if you'd like.

    Thanks!


    Ed Price (a.k.a User Ed), SQL Server Experience Program Manager (Blog, Twitter, Wiki)

    Monday, February 20, 2012 6:24 AM