-1

How can I update password or other attributes in connection string in XML using c# .

<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="abcConnection" connectionString="server=10.10.12.12;database=Test1;uid=myUI‌​D;password=hello;tim‌​eout=20;"providerNam‌​e="System.Data.SqlCl‌​ient" />
        <add name="123Connection" connectionString="server=10.10.23.45;database=test2;uid=MyUS‌​I;password=hello;" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

it can be any xml file not the config from the same application . Please help.

Paweł Dyl
  • 8,888
  • 1
  • 11
  • 27

1 Answers1

0

You can Update your xml anyway:

use this reference:

 using System.IO;

use this code:

   var XMLpath = "c:\test.xml"; //<< your xml here
    if (!File.Exists(XMLpath))
    {
        using (StreamWriter XmlWrite = new StreamWriter(XMLpath, false))
        {
            string[] x = {@"<?xml version=""1.0""?>",
                           @"<configuration>",
                           @"<connectionStrings>",
                           @"<add name=""abcConnection"" connectionString=""server=10.10.12.12;database=Test1;uid=myUI‌​D;password=hello;tim‌​eout=20;""providerNam‌​e=""System.Data.SqlCl‌​ient"" />""",
                           @"<add name=""123Connection"" connectionString=""server=10.10.23.45;database=test2;uid=MyUS‌​I;password=hello;"" providerName=""System.Data.SqlClient"" />",
                           @"</connectionStrings>",
                           @"</configuration>}";
            foreach (string s in x)
            {
                XmlWrite.WriteLine(s);
            }
        }
    }
JohnLiz15
  • 55
  • 11