使用 powershell/.NET 附加节点问题
Appending node issue with powershell/ .NET
我整天都在处理这个问题,似乎无法调整基于 .NET 的解决方案或本机 Powershell 解决方案的答案来满足我的需要。
这基本上是我的问题。我有几组不同的 XML 节点,它们的子节点是这样的:
[xml]$parentContainer = @"
<?xml version="1.0"?>
<parentcontainer></parentcontainer>
"@
[xml]$parent = @"
<parent>
<attribute>someattribute</attribute>
<childcontainer></childcontainer>
</parent>
"@
[xml]$child = @"
<child>
<childattr>something</childattr>
<boolval>yes</boolval>
</child>
"@
这些片段中的每一个都由 PowerShell 内部的关联 class 返回,我的目标是最终将它们全部合并到一个文档中。所以它可能看起来像这样:
<?xml version="1.0"?>
<parentcontainer>
<parent>
<attribute>someattribute</attribute>
<childcontainer>
<child>
<childattr>something</childattr>
<boolval>yes</boolval>
</child>
<child>
<childattr>something</childattr>
<boolval>yes</boolval>
</child>
</childcontainer>
</parent>
</parentcontainer>
我已经尝试了一些方法,包括将子节点作为片段附加,将节点导入到一个公共文档中,使用 XDocument classes 而不是 XmlDocument classes 但没有什么能完全得到我在那里。我的一些节点被 PowerShell 解释为字符串,所以我不能在它们上面使用基于节点的函数,父节点被解释为文档而不是节点,所以我在尝试附加它们时丢失了顶层,等等......
在 PowerShell 或 .NET 中执行此类操作的最佳方法是什么?
试试这个:
[xml]$parentContainer = @"
<?xml version="1.0"?>
<parentcontainer></parentcontainer>
"@
[xml]$parent = @"
<parent>
<attribute>someattribute</attribute>
<childcontainer></childcontainer>
</parent>
"@
[xml]$child = @"
<child>
<childattr>something</childattr>
<boolval>yes</boolval>
</child>
"@
#Add Node child 1 to parent
$xpath = '//parent/childcontainer'
$childnode1 = $parent.SelectSingleNode($xpath)
$childnode1.AppendChild($parent.ImportNode(($child.child), $true));
#Add Node child 2 to parent
$childnode2 = $parent.SelectSingleNode($xpath)
$childnode2.AppendChild($parent.ImportNode(($child.child), $true));
#Add Node parent to parentcontainer
$xpath = '//parentcontainer'
$childnode = $parentContainer.SelectSingleNode($xpath)
$childnode.AppendChild($parentContainer.ImportNode(($parent.parent), $true));
#show result
$parentcontainer.InnerXml
我整天都在处理这个问题,似乎无法调整基于 .NET 的解决方案或本机 Powershell 解决方案的答案来满足我的需要。
这基本上是我的问题。我有几组不同的 XML 节点,它们的子节点是这样的:
[xml]$parentContainer = @"
<?xml version="1.0"?>
<parentcontainer></parentcontainer>
"@
[xml]$parent = @"
<parent>
<attribute>someattribute</attribute>
<childcontainer></childcontainer>
</parent>
"@
[xml]$child = @"
<child>
<childattr>something</childattr>
<boolval>yes</boolval>
</child>
"@
这些片段中的每一个都由 PowerShell 内部的关联 class 返回,我的目标是最终将它们全部合并到一个文档中。所以它可能看起来像这样:
<?xml version="1.0"?>
<parentcontainer>
<parent>
<attribute>someattribute</attribute>
<childcontainer>
<child>
<childattr>something</childattr>
<boolval>yes</boolval>
</child>
<child>
<childattr>something</childattr>
<boolval>yes</boolval>
</child>
</childcontainer>
</parent>
</parentcontainer>
我已经尝试了一些方法,包括将子节点作为片段附加,将节点导入到一个公共文档中,使用 XDocument classes 而不是 XmlDocument classes 但没有什么能完全得到我在那里。我的一些节点被 PowerShell 解释为字符串,所以我不能在它们上面使用基于节点的函数,父节点被解释为文档而不是节点,所以我在尝试附加它们时丢失了顶层,等等......
在 PowerShell 或 .NET 中执行此类操作的最佳方法是什么?
试试这个:
[xml]$parentContainer = @"
<?xml version="1.0"?>
<parentcontainer></parentcontainer>
"@
[xml]$parent = @"
<parent>
<attribute>someattribute</attribute>
<childcontainer></childcontainer>
</parent>
"@
[xml]$child = @"
<child>
<childattr>something</childattr>
<boolval>yes</boolval>
</child>
"@
#Add Node child 1 to parent
$xpath = '//parent/childcontainer'
$childnode1 = $parent.SelectSingleNode($xpath)
$childnode1.AppendChild($parent.ImportNode(($child.child), $true));
#Add Node child 2 to parent
$childnode2 = $parent.SelectSingleNode($xpath)
$childnode2.AppendChild($parent.ImportNode(($child.child), $true));
#Add Node parent to parentcontainer
$xpath = '//parentcontainer'
$childnode = $parentContainer.SelectSingleNode($xpath)
$childnode.AppendChild($parentContainer.ImportNode(($parent.parent), $true));
#show result
$parentcontainer.InnerXml