bderudsmuho
New Member
I'm trying to write an script that modifies an XML file. I've wrote a function that searches the element using Xpath and modifies it:Function UpdateText ([System.Xml.XmlDocument] $xml, [string] $path, [string] $value) { # $node = Select-XML -XML $xml -XPath $path $node = $xml.SelectSingleNode("//$path") if ($node -ne $null) { Write-Host "OK `t $path -> `"$value`"" $node.InnerText=$value } else { # Need to create element Write-Host "ERROR `t $path doesn't exist!" }}This works very well if the element/path exists. For example, with this XML file:<settings> <musicplayer> <crossfade>0</crossfade> <queuebydefault>false</queuebydefault> </musicplayer></settings>I can change a value using a call like this:UpdateText $myXML "settings/musicplayer/crossfade" "1"But, how can I create the element if it doesn't exist? Like this:UpdateText $myXML "settings/audio/volume" "50"without using some kind of recursive function?Thanks.