我需要修改xml文件中的多个路径。我需要读取所有的路径,然后将我的系统信息添加到每个路径中。我如何读取所有路径,将它们存储在一个变量中,然后更新它们。$path= $newText + oldPath
<monitor>
<fileProcessor>
<pathConfiguration>
<path>/path/to/file</path>
</pathConfiguration>
</fileProcessor>
</monitor>
$newText= "C:/users/name"
foreach ($element in $xmlDoc.monitor.fileProcessor.pathConfiguration.path)
{
$element = $newText + existingText
}
尝试设置InnerText属性。如下所示:
$XML =
[XML]@"
<monitor>
<fileProcessor>
<pathConfiguration>
<path>/path/to/file</path>
</pathConfiguration>
</fileProcessor>
</monitor>
"@
$newText = "C:/users/name/"
ForEach($Path in $XML.monitor.fileProcessor.pathConfiguration.ChildNodes )
{
$Path.InnerText = $newText + $Path
}
你当然也可以使用ForEach-Object
。以下是您在问题中提到的使用变量赋值的示例:
$XML =
[XML]@"
<monitor>
<fileProcessor>
<pathConfiguration>
<path>/path/to/file</path>
</pathConfiguration>
</fileProcessor>
</monitor>
"@
$newText = "C:/users/name/"
$ChildPathNodes = $XML.monitor.fileProcessor.pathConfiguration.ChildNodes
$ChildPathNodes |
ForEach-Object{
$_.InnerText = $newText + $_.InnerText
}
你可以检查结果:$XML.monitor.fileProcessor.pathConfiguration