【发布时间】:2011-08-26 19:30:32
【问题描述】:
首先,我要提一下,我是 PowerShell 的初学者,在此先感谢大家的帮助
我在 PowerShell 脚本中有一个函数,它使用
创建大量新的 XML 节点$fileElement = $xml.CreateElement("FileRef")
$fileElement.SetAttribute("Id",$refId)
这很好用,所以我有几个名为 FileRef 的节点兄弟节点,每个节点都有不同的 Id 属性。 $fileElement 变量稍后在脚本中再次使用,它使用 $fileElement.AppendChild 添加它自己的子节点。
我现在有一个循环调用脚本函数多次传入同一个 $refId 变量的情况。我遇到的问题是输出正在获得具有相同 ID 的重复 FileRef 节点兄弟姐妹。
在某些情况下,我想做的是从现有的 FileRef 节点兄弟节点创建 $fileElement 变量,其 id = $refId(这样我以后仍然可以在脚本中对变量使用 AppendChild)使用 id = $refId 创建一个新的 XML 节点兄弟(这会导致重复)。例如
if(circumstances)
{
# first call to the function in the loop, so create new node sibling
$fileElement = $xml.CreateElement("FileRef")
$fileElement.SetAttribute("Id",$refId)
}
else
{
# node sibling already exists, do not create new node,
# use existing node sibling with id = $refId
create xml node variable $fileElement here
}
谢谢
【问题讨论】:
标签: xml powershell