且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 PowerShell 更改 XML 元素值

更新时间:2023-11-07 11:54:28

InnerText 是一个属性,而不是一个方法.它是这样使用的:

InnerText is a property, not a method. It's used like this:

$element.InnerText = "newtext"

此外,我怀疑您的原始数据(与您发布的 XML 示例不同)使用了命名空间.AFAICS 这是 $xml.SelectSingleNode('//Arguments') 返回空结果的唯一可能原因.从 Windows 任务计划程序导出的 XML 文件肯定是命名空间的:

Also, I suspect that your original data (unlike the XML sample you posted) uses namespaces. AFAICS that's the only possible reason why $xml.SelectSingleNode('//Arguments') would return an empty result. XML files exported from the Windows Task Scheduler definitely are namespaced:

<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task‌​">
  <!-- ... -->
</Task>

命名空间与其他节点属性不同,它不仅影响节点本身,还影响其子节点.要从具有命名空间的 XML 中选择节点,您需要一个命名空间管理器:

Namespaces are not like other node attributes and affect not only the node itself, but also its child nodes. For selecting nodes from an XML with namespaces you need a namespace manager:

$nsm = New-Object Xml.XmlNamespaceManager($xml.NameTable)
$nsm.AddNamespace('ns', $xml.DocumentElement.NamespaceURI)
$element = $xml.SelectSingleNode('//ns:Arguments', $nsm)