如何使用c#更新以下XML中的密码或其他属性

时间:2017-03-21 07:18:11

标签: c# xml

如何使用c#更新XML中连接字符串中的密码或其他属性。

$array = @("/website","//windows_service","/console_app","//windows","///IIS","test")
$arraysplit = $array.split(',');
Foreach ($string in $arraysplit)
{
    if ($string.StartsWith("//"))
    {
        Write-Host "$string has two slash."
    }
    elseif($string.StartsWith("/"))
    {
        Write-Host "$string has one slashes."
    }
    else
    {
        #I want to exit only when below conditions meet
        #1. if string doesnot have any slash or
        #2. if string has more than two slashes
        Write-Host "$string has more number of slashes or it doesnot have any slash. Exiting"
        Exit -1
    }
}

它可以是任何xml文件而不是来自同一应用程序的配置。 请帮忙。

1 个答案:

答案 0 :(得分:0)

您仍可以更新xml:

使用此参考:

 using System.IO;

使用此代码:

   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);
            }
        }
    }