0

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>

3
  • Why is it a problem that &apos; is changed? A regular ' is fine in XML. Also, I don't see it in your example, nor do I understand what's the results for <cloneof>. Looks all fine to me... (Maybe add some small "before", "after" and "expected" examples?)
    – Arjan
    Aug 24, 2017 at 12:01
  • And is <cloneof> always empty in the original file when splitted in two lines?
    – Arjan
    Aug 24, 2017 at 12:08
  • My fault, the script are working the problem i list previously are only aesthetic since i would to leave untouched the rest of the xml. Yes the cloneof node empty but it come in two line... it remain in a single line if the node as some text inside I will put an exmple in the first post to be much clear :D
    – Moreno
    Aug 24, 2017 at 12:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.