Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用PowerShell,什么';这是给孩子增加三层深度的最简单的方法吗?_Xml_Powershell - Fatal编程技术网

Xml 使用PowerShell,什么';这是给孩子增加三层深度的最简单的方法吗?

Xml 使用PowerShell,什么';这是给孩子增加三层深度的最简单的方法吗?,xml,powershell,Xml,Powershell,给定以下XML: <greatGrandParent> </greatGrandParent> 我想以以下方式结束: <greatGrandparent> <grandparent> <parent> <child /> </parent> </grandparent> </greatGrandparent> 在PowerShell中,添加最

给定以下XML:

<greatGrandParent>
</greatGrandParent>

我想以以下方式结束:

<greatGrandparent>
  <grandparent>
    <parent>
      <child />
    </parent>
  </grandparent>
</greatGrandparent>

在PowerShell中,添加最后一个节点(子节点)的最简单方法是什么?仅使用路径/曾祖父母/祖父母/父母/子女就可以完成吗?在没有曾祖父母、祖父母、祖父母和祖父母等的情况下,能做到这一点吗

下面是我用来自动构建路径中缺失节点的示例。CreateNode方法不能在路径中使用“/”,因此不能用于构建节点

$path = "C:\xmlfile1.xml"

$content = '<greatGrandParent></greatGrandParent>'
[System.Xml.XmlDocument] $xd = new-object System.Xml.XmlDocument
$xd.LoadXml($content)

$nodes = @("greatGrandParent", "grandParent", "parent", "children", "add[@key='John']", "add")

$finalNode = $null
$currentNode = $xd
$currentXpath = ""

foreach ($nodeName in $nodes) {
    $currentXpath += "/" + $nodeName
    $finalNode = $xd.SelectSingleNode($currentXpath)
    if (!$finalNode) {
        if ($nodeName -eq $nodes[$nodes.Count-2]) {
            $finalNode = $xd.CreateNode("element", $nodes[$nodes.Count-1], "")
            $currentNode.AppendChild($finalNode)
            break
        }
        else {
            $finalNode = $xd.CreateNode("element", $nodeName, "")
            $currentNode.AppendChild($finalNode)
        }
    }
    $currentNode = $finalNode
}

$xd.Save($path)

Write-Host "Complete."
$path=“C:\xmlfile1.xml”
$content=''
[System.Xml.XmlDocument]$xd=新对象System.Xml.XmlDocument
$xd.LoadXml($content)
$nodes=@(“曾祖父母”、“祖父母”、“父母”、“子女”、“添加[@key='John'],“添加”)
$finalNode=$null
$currentNode=$xd
$currentXpath=“”
foreach($nodesname in$nodes){
$currentXpath+=“/”+$nodeName
$finalNode=$xd.SelectSingleNode($currentXpath)
如果(!$finalNode){
if($nodeName-eq$nodes[$nodes.Count-2]){
$finalNode=$xd.CreateNode(“元素”,$nodes[$nodes.Count-1],“”)
$currentNode.AppendChild($finalNode)
打破
}
否则{
$finalNode=$xd.CreateNode(“元素”、$nodeName“”)
$currentNode.AppendChild($finalNode)
}
}
$currentNode=$finalNode
}
$xd.Save($path)
写主机“完成”

您可以先添加一个节点,然后再添加一个节点。

请分享您尝试的解决方案,这不是一项代码编写服务。