Search code examples
c#asp.netweb-config

Read and replace Values of a particular tag in web.config stored at a different project locations in C#


I need to write a program where i can replace value of a praticular tag of web.config file of multiple projects locations in the given config.xml.

config.xml

<?xml version="1.0" encoding="utf-8" ?>
<ReplaceFile>
<File>
<tag>connectionStrings</tag>
<FilePath>C:\Users\abc\Documents\Visual Studio2013\Projects\WebApplication1\WebApplication1\web.config</FilePath>
<Repstring>"some string "</Repstring>
</File>
<File>
<tag>connectionStrings</tag>
<FilePath>C:\Users\abc\Documents\Visual Studio 2013\Projects\WebApplication2\WebApplication2\web.config</FilePath>
<Repstring>" some string "</Repstring>
</File>     
</ReplaceFile>

Web.config

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="FEConnectionString" connectionString="Provider=xyz;Data Source=xyz;User id=xyz;Password=xyz" providerName="xyz"/>
  </connectionStrings>
 </configuration>

so my task is to check for the all the web.config file of config.xml and search for connectionStrings node and under the node connectionstring replace connectionstring. i am new to C#.

MYcode

            static void UpdateXML()
        {
            XElement xdoc = XElement.Load("C:\\Users\\abc\\Desktop\\Config.xml");
            IEnumerable<XElement> config = xdoc.Elements();
            foreach (var file in config)
            {
                string tagname = file.Element("tag").Value;
                string FilePath = file.Element("FilePath").Value;
                string Repstring = file.Element("Repstring").Value;
                //Console.WriteLine(tagname);
                //Console.WriteLine(FilePath);
                //Console.WriteLine(Repstring);
                XElement xwebconfig = XElement.Load(FilePath);
                IEnumerable<XElement> webconfig1 = xwebconfig.Elements();
                foreach (var node in webconfig1)
                { 
                  Console.WriteLine(node);
                }
            }
        }

As far as I understand ConfigurationManager is specific to that particular project so I cant use that namespace.

Any kind of help is appreciated .


Solution

  • Here are two ways to update a connectionStrings in a web.config file.

    The first method

    XDocument xwebconfig = XDocument.Load(FilePath);
    XElement xElement = xwebconfig.Elements("configuration").Elements("connectionStrings").Elements("add").FirstOrDefault();
    xElement.SetAttributeValue("name", "myconnection");
    xwebconfig.Save(FilePath);
    

    The second method

    XDocument xwebconfig = XDocument.Load(@"C:\Users\Administrator\Desktop\Web.config");
    IEnumerable<XElement> webconfig1 = xwebconfig.Elements();
    foreach (var node in webconfig1)
    {
        foreach (var subnode1 in node.Elements())
        {
           if(subnode1.Name == "connectionStrings")
           {
              foreach (var subnode2 in subnode1.Elements())
              {
                 subnode2.SetAttributeValue("name", "myconnection");
              }
           }
       }
    }
    xwebconfig.Save(@"C:\Users\Administrator\Desktop\Web.config");