Powershell script keep deleting ' entity

I have made a powershell script to edit multiple xml file in a folder,
it will edit only the “description” node but for some reason the final result is that the ' entity became a normal ' and the “cloneof” node is splitted in two lines.

Get-ChildItem .\ *.xml -recurse | % {
echo "Editing file" $_.Name
$xml = [xml](Get-Content $_.fullname)
$xml.SelectNodes("//description") | ForEach-Object {
$string = $_.InnerText

if ($string -like "*(*") {
    $string = $string.substring(0, $string.Indexof("(")-1)

    if ($string -like '*,*') 
        {
        $article = $string.Substring($string.LastIndexOf(','))

        if ([string]$article -eq ", The") {
            $string = $article.Substring(2) + " " + $string.Substring(0, $string.LastIndexOf(","))
        }
        elseif ([string]$article -eq ", A") {
            $string = $article.Substring(2) + " " + $string.Substring(0, $string.LastIndexOf(","))
        }
        elseif ([string]$article -eq ", L'") {
            $string = $article.Substring(2) + $string.Substring(0, $string.LastIndexOf(","))
        }
        elseif ([string]$article -like "*-*") {
            $string = $article.Substring(2, $article.IndexOf("-")-2) + $string.Substring(0, $string.LastIndexOf(",")) + $article.substring($article.IndexOf("-")-1)
        }


      else {
            echo "No article renaming needed for:"
            echo $article
            echo $string
           }

        }
    }

$_.'#text' = $string
} 
$xml.save($_.FullName)
}

some ideas? teorically the rest of the file will remain untouched right?

the XML is full of section like this
ORIGINAL XML

<game name="Addams Family, The - Pugsley&apos;s Scavenger Hunt (USA)" index="" image="">
    <description>Addams Family, The - Pugsley&apos;s Scavenger Hunt (USA)</description>
    <cloneof></cloneof>
    <crc>6B8D777D</crc>
    <manufacturer>Ocean</manufacturer>
    <year>1993</year>
    <genre>Platform</genre>
    <rating>Other - NR (Not Rated)</rating>
    <enabled>Yes</enabled>
</game>

EDITED XML

<game name="Addams Family, The - Pugsley's Scavenger Hunt (USA)" index="" image="">
<description>The Addams Family - Pugsley's Scavenger Hunt</description>
<cloneof>
</cloneof>
<crc>6B8D777D</crc>
<manufacturer>Ocean</manufacturer>
<year>1993</year>
<genre>Platform</genre>
<rating>Other - NR (Not Rated)</rating>
<enabled>Yes</enabled>

Answer

Attribution
Source : Link , Question Author : Moreno , Answer Author : Community

Leave a Comment