提问人:TheRob87 提问时间:9/28/2023 更新时间:9/28/2023 访问量:33
Powershell 选择复制粘贴 XML 节点
Powershell Select Copy Paste XML NODE
问:
我有一个XMl,我想扩展它。
- 我想选择 2.“Step” 节点 - > 复制 -> 更改 -Number- |-Name- 和一些内部文本字符串
- 我想选择 2.“Transition” 节点 -> copy -> 更改 -Number- |-Name- 和一些内部文本字符串
- 我想选择 2.“Connection” Node -> copy -> 更改一些内部文本字符串
- 我想选择倒数第二个“连接”副本 -> 更改一些内部文本字符串
这是我的XML(没有结束标签):
以下是在其他作业中工作正常的代码:
$OPN_in ='C:\OPN\OPNOUT.xml'
$OPN_Out='C:\OPN\imp\OPNIMP.xml'
# xml Objekt erstellen
$xml = New-Object XML
# xml laden
$xml.Load($OPN_in)
# Abfrage des ersten 'SW.Blocks.CompileUnit' Knotens
$Bsp_NW = $xml.SelectSingleNode("//SW.Blocks.CompileUnit")
# abbrechen wenn kein Knoten gefunden wurde.
if (!$Bsp_NW){
throw "Error, 'SW.Blocks.CompileUnit' Node not found!"
exit 1
}
# erstelle eine Kopie des Knotens im Speicher
$NEU_NW = $Bsp_NW.Clone()
$NEU_NW.innerxml = $NEU_NW.innerxml -replace ("'Start'" -replace "'"),("'NEXT'" -replace "'")
# id anpassen
$NEU_NW.Id = (100*$z).ToString()
# hänge den kopierten Knoten im selben Parent wie vom Original wieder ins XML ein
$Bsp_NW.ParentNode.InsertAfter($NEU_NW,$Bsp_NW)
#Basisdatenändern
$xml.Document.'SW.Blocks.FC'.AttributeList.Name ='OPN_NEW'
$xml.Document.'SW.Blocks.FC'.AttributeList.Number = [String]($xml.Document.'SW.Blocks.FC'.AttributeList.Number+'00')
# speichere das geänderte xml unter neuem Namen
$xml.Save($OPN_Out)
#>
不起作用的是:
$Bsp_NW = $xml.SelectSingleNode("//Steps")
感谢您的支持。
答:
0赞
TheRob87
9/28/2023
#1
现在它正在与:
# Abfrage des ersten 'SW.Blocks.CompileUnit' Knotens
$Bsp_NW = $xml.SelectSingleNode("//SW.Blocks.CompileUnit")
$Bsp_NW = $xml.SelectSingleNode("//Steps")
$NEW_Step = $Bsp_NW.step[1].Clone()
$NEW_Step.Number
我很困惑......
但是也许你可以给我一个提示 如何使用模具“数字”属性选择步骤?
评论
0赞
TheRob87
9/29/2023
stackoverflow.com/questions/27841491/......
评论
$xml = [xml] "<Document><SW.Blocks.FB><SW.Blocks.CompileUnit><AttributeList><NetworkSource><PreOperations><Sequence><Steps><Step /><Step /></Steps></Sequence></PreOperations></NetworkSource></AttributeList></SW.Blocks.CompileUnit></SW.Blocks.FB></Document>"; $xml.SelectSIngleNode("//Steps")
$xml