0

I've been doing quite a bit of searching/experimenting, and I know similar problems have been solved, but I'm struggling with this problem specifically and am reaching out for another pair of eyes. My goal is to parse an .xml file for names, alter the names, then save them. My .xml in brief:

<?xml version="1.0"?>
<menu>
    <header>
        <listname>Thing</listname>
        <lastlistupdate>11/15/2011</lastlistupdate>
        <listversion>1.1 Final</listversion>
        <exporterversion>12.0</exporterversion>
    </header>
    <game name="My - Game (Japan) (Eu,Br)" index="true" image="0">
        <description>My - Game (Japan) (Eu,Br)</description>
        <cloneof></cloneof>
        <crc>8B70CB5B</crc>
        <manufacturer>Manufacturer</manufacturer>
        <year>1997</year>
        <genre>Shooter</genre>
        <rating>ESRB - T (Teen)</rating>
        <enabled>Yes</enabled>
    </game>
    <game name="Some Other Cool Game (Stuff) (Foo) (Bar)" index="" image="">
        <description>Some Other Cool Game (Stuff) (Foo) (Bar)</description>
        <cloneof></cloneof>
        <crc>34F0EE5B</crc>
        <manufacturer>Manufacturer</manufacturer>
        <year>2000</year>
        <genre>Shooter</genre>
        <rating>ESRB - T (Teen)</rating>
        <enabled>Yes</enabled>
    </game>

I need to remove everything after the first "(" in the "game name" field and replace this content with "(USA)", so the first game name needs to change from:

My - Game (Japan) (Eu,Br)
Some Other Cool Game (Stuff) (Foo) (Bar)

To:

My - Game (USA)
Some Other Cool Game (USA)

I'm pretty close, and I'm able to get Powershell to print out the corrected names with:

[xml]$gameNames = Get-Content '.\myXML.xml' $gameNames.menu.game | foreach { $_.Name -replace '(.*', "(USA)" }

What I can't figure out is how to save hese new names in the "game name" nodes. I tried adding | Set-Content '.\myXML.xml' to the end of my pipe, but this just overwrites the original .xml with the renamed game names and none of the original data.

*Edit: There is a "\" before the "(" in the last -replace line. It doesn't show up, but (without spaces), the line should look like: -replace ' \ ( . * '

1 Answer 1

1
$gameNames.menu.game | foreach { $_.Name = $_.Name -replace '\(.*', "(USA)" }
3
  • Why has this been voted to be deleted? It works.
    – Martin
    Commented Oct 14, 2013 at 13:45
  • I have no idea. I have not yet been able to verify that this works but it looks correct! I will update you later, thanks!
    – root
    Commented Oct 14, 2013 at 14:30
  • That was it! The final step here was to correctly save my changes. Here's what my final version was: [xml]$gameNames = Get-Content '.\File.xml' $gameNames.menu.game | foreach { $_.Name = $_.Name -replace '\(.*', "(USA)" } $gameNames.Save("C:\File.xml")
    – root
    Commented Oct 14, 2013 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .