0

So I'm learning how to manipulate an xml document with powershell. Right now I'm just trying to change a give node and then save the changes. I'm currently stuck at how to save my changes.

Here's what i have.

$xmlfile = "testFile.xml"

$xml = [xml](get-content $xmlfile)
$employee = $xml.employees.employee
$employee[1].name = "New Name" // this is where I change the content of the xml file
//is this an okay way to change the value of the element??
$xml.save($xmlfile) //why wouldn't this line save my changes??

Thanks for your help :)

Jeff
  • 3,943
  • 8
  • 45
  • 68
  • I think my script is write the problem is that my Save command has saved my file to `home` not the current working directory --- http://stackoverflow.com/questions/4822575/saving-an-xml-file-in-powershell-requires-complete-path-why – Jeff Mar 29 '12 at 22:02

1 Answers1

2

You need to pass the full path to the save method (e.g. $xml.save((Resolve-Path $xmlfile))) When you cast a variable to [xml] in powershell it is loading the xml into an XmlDocument object, which is part of the. NET Framework. It is not aware of powershell so it does not know what directory your shell is currently in. So your code above is saving the document, but not in the place you expect.

Andy
  • 411
  • 3
  • 5