【问题标题】:changing and xml document with powershell and then saving使用powershell更改和xml文档然后保存
【发布时间】:2012-03-30 06:00:44
【问题描述】:

所以我正在学习如何使用 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??

感谢您的帮助:)

【问题讨论】:

标签: xml powershell


【解决方案1】:

您需要将完整路径传递给保存方法(例如 $xml.save((Resolve-Path $xmlfile)))当您在 powershell 中将变量转换为 [xml] 时,它会将 xml 加载到 XmlDocument 对象中,这是其中的一部分。 NET 框架。它不知道 powershell,因此它不知道您的 shell 当前位于哪个目录。因此,您上面的代码正在保存文档,但不在您期望的位置。

【讨论】:

  • 它保存到我的个人资料目录 (C:\Users\Andy)。我的 PWD 是桌面。
猜你喜欢
  • 2010-12-15
  • 2019-10-07
  • 2011-08-27
  • 2011-03-11
  • 2021-05-15
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多