我的XML文档如下
<Batch>
<Alter>
<ObjectDefinition>
<DataSources>
<DataSource>
<ConnectionString>Provider=SQLNCLI11.1;Data Source=ABC;Integrated Security=SSPI;Initial Catalog =TesDB1</ConnectionString>
<ConnectionString>Provider=SQLNCLI11.1;Data Source=XYZ;Integrated Security=SSPI;Initial Catalog =TesDB1</ConnectionString>
</DataSource>
</DataSources>
</ObjectDefinition>
</Alter>
</Batch>
我想替换整个连接字符串节点,如下所示
$ConnectionString = "Provider=SQLNCLI11.1;Data Source=$DataSource;Integrated Security=SSPI;Initial Catalog =$Database"
这是我尝试过的,但这只是替换了一个节点内容
$XMLA = "D:\employee.xml"
$SqlDataBase = [xml](Get-Content $XMLA)
$data = $SqlDataBase.SelectNodes("Batch/Alter/ObjectDefinition/DataSources/DataSource/ConnectionString")
#$data
$DataSource = "localhost"
foreach($xn in $data)
{
$DataBase = $xn.'#text'.Split(";").Split("=")
$DataBase[$DataBase.Length - 1]
$DataBase = $DataBase[$DataBase.Length - 1]
$ConnectionString = "Provider=SQLNCLI11.1;Data Source=$DataSource;Integrated Security=SSPI;Initial Catalog =$Database"
$SqlDataBase.SelectSingleNode("Batch/Alter/ObjectDefinition/DataSources/DataSource/ConnectionString").InnerText = "$ConnectionString";
}
$SqlDataBase.Save($XMLA)
所以更换多个节点的更好方法请告诉我