Search code examples
xmlpowershellpowershell-4.0

Replace '.00' to blank value in xml with powershell


I have a xml file with tag . However, the value in the tag somehow displayed as 111.01.00 then i want to remove .00. I've tried to replace function but it seems to print to powershell screen instead of saving though i already had save function Here is my code:

param ($folder = 'C:\Users\blitz\Desktop\Baiduri\CR\new_xml_format')
$files = Get-ChildItem -Path $folder -Include "*xml" -Recurse
foreach ($file in $files) {
[xml]$xml = Get-Content $file
$xml.SelectNodes('//*[self::balance]') |
    ForEach-Object { $_.'#text' -replace '.00',''} 
$xml.Save($file)
}

Solution

  • You're replacing the text but you're not actually doing anything with it. Set the item back with the results.

    param ($folder = 'C:\Users\blitz\Desktop\Baiduri\CR\new_xml_format')
    $files = Get-ChildItem -Path $folder -Include "*xml" -Recurse
    foreach ($file in $files) {
    [xml]$xml = Get-Content $file
    $xml.SelectNodes('//*[self::balance]') |
        ForEach-Object { $_.'#text' = $_.'#text' -replace '.00',''} 
    $xml.Save($file)
    }
    

    When you're replacing something with nothing you can omit the ,'' and make it just

    $_.'#text' = $_.'#text' -replace '.00'