问题描述
我需要将名为ProjectOptions"的节点从 default.xml 复制到 original.xml 而不修改任何其他内容:
I need to copy node with name "ProjectOptions" from default.xml to original.xml without modifying anything else:
原始.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<KEYS>
<KEY ObjectName="computername_user" RegObjectType="0">
<KEYS>
<KEY ObjectName="Desktop" RegObjectType="0">
<KEYS>
<KEY ObjectName="Settings" RegObjectType="0">
<KEYS>
<KEY ObjectName="PrinterDefault" RegObjectType="0">
<VALUES>
<VALUE ObjectName="PrinterOrientation" Value="2" ValueType="4" />
</VALUES>
</KEY>
<KEY ObjectName="ProjectOptions" RegObjectType="0">
<VALUES>
<VALUE ObjectName="ShowWelcomeMsg" Value="0" ValueType="4" />
</VALUES>
</KEY>
</KEYS>
</KEY>
</KEYS>
</KEY>
</KEYS>
</KEY>
</KEYS>
默认.xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<KEYS>
<KEY ObjectName="computername_user" RegObjectType="0">
<KEYS>
<KEY ObjectName="Desktop" RegObjectType="0">
<KEYS>
<KEY ObjectName="Settings" RegObjectType="0">
<KEYS>
<KEY ObjectName="PrinterDefault" RegObjectType="0">
<VALUES>
<VALUE ObjectName="PrinterOrientation" Value="2" ValueType="4"/>
</VALUES>
</KEY>
<KEY ObjectName="ProjectOptions" RegObjectType="0">
<VALUES>
<VALUE ObjectName="GSAddBatchOptionDialogRect" Value="381,203,981,629" ValueType="2"/>
<VALUE ObjectName="GSHeadNodeName" Value="" ValueType="2"/>
<VALUE ObjectName="GSIsAdvancedMode" Value="1" ValueType="4"/>
<VALUE ObjectName="GSRemoteSchedulerPlatform" Value="" ValueType="2"/>
<VALUE ObjectName="GSSchedulerName" Value="" ValueType="2"/>
<VALUE ObjectName="GSShowFrequentlyUsedBatchOptions" Value="1" ValueType="4"/>
<VALUE ObjectName="GSUserName" Value="" ValueType="2"/>
<VALUE ObjectName="ShowWelcomeMsg" Value="0" ValueType="4"/>
</VALUES>
</KEY>
</KEYS>
</KEY>
</KEYS>
</KEY>
</KEYS>
</KEY>
</KEYS>
我试过这样的事情
$xml = [xml](Get-Content "C:\Temp\original.xml")
$xmld = [xml](Get-Content "C:\Temp\default.xml")
$Child=$xml.KEYS.KEY.KEYS.KEY.KEYS.KEY.KEYS.KEY[1].VALUES.VALUE
$xml.DocumentElement.InsertAfter($XML.ImportNode($xmld.SelectSingleNode("//KEY[@ObjectName = 'ProjectOptions']"), $true), $Child)
$xml.Save("C:\Temp\save.xml")
但它以参考节点不是此节点的子节点"结尾.请告诉我我哪里出错了.谢谢.
But it ended with "The reference node is not a child of this node."Please tell me where I went wrong.Thanks.
推荐答案
你尝试在 DocumentElement
节点下插入导入的节点,但是 $Child
不是直接该节点的子元素.需要在$Child
的父节点上调用InsertAfter()
方法.
You try to insert the imported node under the DocumentElement
node, but $Child
is not a direct child element of that node. You need to call the InsertAfter()
method on the parent node of $Child
.
改变这个:
$xml.DocumentElement.InsertAfter($XML.ImportNode($xmld.SelectSingleNode("//KEY[@ObjectName = 'ProjectOptions']"), $true), $Child)
进入这个:
$Child.ParentNode.InsertAfter($XML.ImportNode($xmld.SelectSingleNode("//KEY[@ObjectName='ProjectOptions']"), $true), $Child)
问题就会消失.
作为旁注,您可能希望使用 XPath 表达式而不是点符号来选择 $Child
:
As a side note, you may want to use an XPath expression instead of dot-notation for selecting $Child
:
$Child = $xml.SelectSingleNode('//VALUE[@ObjectName="ShowWelcomeMsg"]')
这篇关于在 PowerShell 上将节点从一个 XML 导入到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!