0

I'm trying to replace URLs inside XML. e.g.

http://sample.com/v2/some/action 

to

http://sample-v2.com/v2/some/action

XML sample:

<someroot>
    <serice>
        <action url="http://sample.com/v2/some/action" />
    </service>
</someroot>

When I'm running this script:

$path = "..."
$line = "http://sample.com/v2/some/action"
$replacement = "http://sample-v2.com/v2/some/action"
(Get-Content $path) | Foreach-Object {
    $_ -replace $line, $replacement
}  | Out-File ($path + ".replaced.xml")

it doesn't work. What am I doing wrong?

UPDATE:

The actual code:

foreach ($line in $inputLines) {
    $replacement = $line.Replace("sample.test", "sample-" + $suffix + ".test")

    Write-Host ("Current input line:", $line) -ForegroundColor Yellow
    Write-Host ("Will be replaced with:", $replacement) -ForegroundColor Yellow

    (Get-Content $path) | Foreach-Object { 
        $_.Replace($line, $replacement) 
    }  | Out-File ($path + ".replaced.xml")
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Nikita Fedorov
  • 790
  • 1
  • 12
  • 40
  • Looks fine. Tested and it works for me. What exactly is your problem? Is this the actual code? Note that `-replace` uses regex matching! Maybe try `$_.Replace($line, $replacement)` instead – marsze Jan 29 '19 at 13:09
  • worked with $_.Replace, but still doesn't in the main code – Nikita Fedorov Jan 29 '19 at 13:39
  • 2
    You still didn't say what exactly the problem is. I guess nothing is replaced? You should provide some of the actual output. I am sure the problem is somewhere else. – marsze Jan 29 '19 at 13:41
  • [**DO NOT PARSE XML WITH REGEX**](https://stackoverflow.com/a/1732454/1630171). – Ansgar Wiechers Jan 29 '19 at 18:08

0 Answers0