0

In my example XML file, i need to replace the two Live DB Server variables with two new variables:

$newdb1 = "newproductiondb1"
$newdb2 = "newproductiondb2" 

My current XML File:

<?xml version="1.0" encoding="UTF-8" ?>
<databases>
 <database name="Live">
 <db1 server="productiondb1" dbname="Live1" />
 <db2 server="productiondb2" dbname="Live2" />
 </database>
 <database name="UAT">
 <db1 server="uatdb1" dbname="UAT1" />
 <db2 server="uatdb2" dbname="UAT2" />
 </database>
 <database name="QA">
 <db1 server="qadb1" dbname="QA1" /> 
 <db2 server="qadb2" dbname="QA2" />
</databases>

I cant find the syntax to replace the two variables ('db server' and 'db2 server') within the file just for the Live section.

1 Answer 1

1

Here is an example how you can change this attributes:

$xml = [xml](Get-Content -Path C:\temp\test.xml)
$xml.SelectNodes("//database[@name='Live']") | foreach{
    $_.db1.server = "NewName1"
    $_.db2.server = "NewName2"
}
# Pretty Print and save File
$XmlWriterSettings = New-Object -TypeName System.Xml.XmlWriterSettings
$XmlWriterSettings.Indent = $true
$XmlWriterSettings.IndentChars = "`t"
$XmlWriterSettings.OmitXmlDeclaration = $true
$XmlWriter = [System.Xml.XmlWriter]::Create("C:\temp\test.xml", $XmlWriterSettings)
$xml.Save($XmlWriter)
$XmlWriter.Close()

Note: xPath is casesensitive

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.