我想在已经存在的其他2个节点之间插入一个节点.在我的脚本中,我收到一个xml变量,我想更新这个.
例如:
<mapping ...>
<INSTANCE .. />
<INSTANCE .. />
<CONNECTOR .. />
<CONNECTOR .. />
</mapping>
Run Code Online (Sandbox Code Playgroud)
结果应该是:
<mapping ...>
<INSTANCE .. />
<INSTANCE .. />
<NEWINSERT .../>
<CONNECTOR .. />
<CONNECTOR .. />
</mapping>
Run Code Online (Sandbox Code Playgroud)
当我使用appendChild时,插件总是在最后完成...
一个主意 ?
谢谢 !
ste*_*tej 12
正如@Grhm回答的那样,你可以做到InsertAfter.我建议总是尝试将其管道Get-Member以获得提示.
> $x = [xml]@"
<mapping>
<INSTANCE a="abc" />
<INSTANCE a="abc" />
<CONNECTOR a="abc" />
<CONNECTOR a="abc" />
</mapping>
"@
> $x | gm -membertype method
TypeName: System.Xml.XmlDocument
Name MemberType Definition
---- ---------- ----------
AppendChild Method System.Xml.XmlNode AppendChild(System.Xml
..
ImportNode Method System.Xml.XmlNode ImportNode(System.Xml.
InsertAfter Method System.Xml.XmlNode InsertAfter(System.Xml
InsertBefore Method System.Xml.XmlNode InsertBefore(System.Xm
Load Method System.Void Load(string filename), System
...
WriteTo Method System.Void WriteTo(System.Xml.XmlWriter
> $newe = $x.CreateElement('newelement')
> $x.mapping.InsertAfter($newe, $x.mapping.INSTANCE[1])
> $x | Format-Custom
Run Code Online (Sandbox Code Playgroud)
我个人认为gm(或Get-Member)是PowerShell中最有用的cmdlet;)
| 归档时间: |
|
| 查看次数: |
5812 次 |
| 最近记录: |