Jeff
Jeff

Reputation: 4003

changing and xml document with powershell and then saving

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 :)

Upvotes: 0

Views: 2309

Answers (1)

Andy
Andy

Reputation: 411

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.

Upvotes: 2

Related Questions