我有一个循环将 XML 元素从一个位置移动到另一个位置。它适用于 For 循环,但对于 Foreach 循环,它会永远迭代。这只是不幸的实现冲突(XMLElement vs foreach 循环)还是我缺少更好的实践?我绝不是 System.Xml 库的专家。
演示脚本
$xmlDocOriginalValue = [xml]@"
<root>
<dir Id = "source" >
<file Id = "1" />
<file Id = "2" />
</dir>
<dir Id = "destination" >
<file Id = "3" />
<file Id = "4" />
</dir>
</root>
"@
$xmlDoc = $xmlDocOriginalValue.Clone()
$files = $xmlDoc.SelectNodes('//file')
$destination = $xmlDoc.SelectSingleNode('//dir[@Id="destination"]')
# Move "files" from source to destination using both a "for" and a "foreach" loop
Write-Host "For loop"
Write-Host "XML before: $($xmlDoc.OuterXml)"
for($i = 0; $i -lt $files.count; $i++)
{
Write-Host ("Iteration "+$i + ", " + $files[$i].OuterXml)
$destination.AppendChild($files[$i]) | Out-Null
}
Write-Host "XML after: $($xmlDoc.OuterXml)"
# Reset and try with a foreach loop
$xmlDoc = $xmlDocOriginalValue.Clone()
$files = $xmlDoc.SelectNodes('//file')
$destination = $xmlDoc.SelectSingleNode('//dir[@Id="destination"]')
Write-Host "`nForeach loop"
Write-Host "XML before: $($xmlDoc.OuterXml)"
$i = 0
foreach($file in $files)
{
Write-Host ("Iteration "+$i + ", " + $file.OuterXml)
$destination.AppendChild($file) | Out-Null
$i++
}
Write-Host "XML after: $($xmlDoc.OuterXml)"
输出
For loop
XML before: <root><dir Id="source"><file Id="1" /><file Id="2" /></dir><dir Id="destination"><file Id="3" /><file Id="4" /></dir></root>
Iteration 0, <file Id="1" />
Iteration 1, <file Id="2" />
Iteration 2, <file Id="3" />
Iteration 3, <file Id="4" />
XML after: <root><dir Id="source"></dir><dir Id="destination"><file Id="1" /><file Id="2" /><file Id="3" /><file Id="4" /></dir></root>
Foreach loop
XML before: <root><dir Id="source"><file Id="1" /><file Id="2" /></dir><dir Id="destination"><file Id="3" /><file Id="4" /></dir></root>
Iteration 0, <file Id="1" />
Iteration 1, <file Id="2" />
Iteration 2, <file Id="3" />
Iteration 3, <file Id="4" />
Iteration 4, <file Id="1" />
Iteration 5, <file Id="2" />
Iteration 6, <file Id="3" />
Iteration 7, <file Id="4" />
Iteration 8, <file Id="1" />
Iteration 9, <file Id="2" />
Iteration 10, <file Id="3" />
Iteration 11, <file Id="4" />
Iteration 12, <file Id="1" />
Iteration 13, <file Id="2" />
Iteration 14, <file Id="3" />
Iteration 15, <file Id="4" />
Iteration 16, <file Id="1" />
Iteration 17, <file Id="2" />
Iteration 18, <file Id="3" />
...