2012-03-29 94 views
0

所以我正在学习如何使用PowerShell处理xml文档。现在我只是想改变一个给定的节点,然后保存更改。我目前被困在如何保存我的更改。使用powershell更改和xml文档,然后保存

这是我的。

$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?? 

感谢您的帮助:)

+0

我觉得我的脚本写的问题是,我的保存命令已经将我的文件保存到'home'而不是当前的工作目录--- http://stackoverflow.com/questions/4822575/saving-an-xml- file-in-powershell-requires-complete-path-why – Jeff 2012-03-29 22:02:10

回答

2

您需要的完整路径传递到保存方法(如xml.save $((解决路径$ XMLFILE)))当你施放一个变量[xml]在powershell中,它将xml加载到XmlDocument对象中,该对象是其中的一部分。 NET框架。它不知道powershell,所以它不知道你的shell目前在什么目录。所以你上面的代码是保存文档,但不是你期望的地方。

+1

它将它保存到我的配置文件目录(C:\ Users \ Andy)。我的PWD是桌面。 – 2012-03-30 03:14:41

相关问题