我真的很努力地操纵PowerShell中的一些XML,我需要将它作为一个主体发送回Web服务。任何人都可以帮助我得到XML需要讨论的方式吗?在另一个节点之后插入一个新的XML节点
<?xml version="1.0" encoding="UTF-8"?>
<EdgeGateway>
<Configuration>
<GatewayInterfaces>
<GatewayInterface>
<InterfaceType>uplink</InterfaceType>
<SubnetParticipation>
<Gateway>1.2.3.4</Gateway>
<Netmask>255.255.255.240</Netmask>
<IpAddress>1.2.3.5</IpAddress>
# Missing the IpRange XML section - defined below
<UseForDefaultRoute>true</UseForDefaultRoute>
</SubnetParticipation>
<UseForDefaultRoute>true</UseForDefaultRoute>
</GatewayInterface>
</GatewayInterfaces>
</Configuration>
</EdgeGateway>
需要成为:
<?xml version="1.0" encoding="UTF-8"?>
<EdgeGateway>
<Configuration>
<GatewayInterfaces>
<GatewayInterface>
<InterfaceType>uplink</InterfaceType>
<SubnetParticipation>
<Gateway>1.2.3.4</Gateway>
<Netmask>255.255.255.240</Netmask>
<IpAddress>1.2.3.5</IpAddress>
# New Content added here
<IpRanges>
<IpRange>
<StartAddress>1.2.3.5</StartAddress>
<EndAddress>1.2.3.5</EndAddress>
<IpRange>
</IpRanges>
# End of new content
<UseForDefaultRoute>true</UseForDefaultRoute>
</SubnetParticipation>
<UseForDefaultRoute>true</UseForDefaultRoute>
</GatewayInterface>
</GatewayInterfaces>
</Configuration>
</EdgeGateway>
到目前为止,我已经能够创造新的内容新的XML节点/元素,但我不能让它在正确的位置插入。我可以使AppendChild()
方法起作用,但它会将该内容放在<UseForDefaultRoute>
部分之后 - 而不是之前。
我试过做InsertBefore()
和InsertAfter()
,但它只是不想工作。 最后,当我做AppendChild()
的方法时,我还得到了一些额外的文本,我没有想到的,关于xmlns的一些?
<IpRanges xmlns=""><IpRange><StartAddress>1.2.3.5</StartAddress><EndAddress>1.2.3.5</EndAddress></IpRange></IpRanges>
这是我好不容易才拼凑,请记住,它的分解:(
# load XML file
[xml]$doc = $response
# create node <StartAddress>
$startNode = $doc.CreateNode('element', 'StartAddress', '')
$start = $doc.CreateTextNode('1.2.3.5')
$startNode.AppendChild($start) | Out-Null
# create node <EndAddress>
$endNode = $doc.CreateNode('element', 'EndAddress', '')
$end = $doc.CreateTextNode('1.2.3.5')
$endNode.AppendChild($end) | Out-Null
# create node <IpRange> and append child nodes <StartAddress> and <EndAddress>
$ipRange = $doc.CreateNode('element', 'IpRange', '')
$ipRange.AppendChild($startNode) | Out-Null
$ipRange.AppendChild($endNode) | Out-Null
# create node <IpRanges> and append child nodes <IpRange>
$ipRanges = $doc.CreateNode('element', 'IpRanges', '')
$ipRanges.AppendChild($ipRange) | Out-Null
# append node <IpRanges> to node <SubnetParticipation>
$subnetParticpation = $doc.EdgeGateway.Configuration.GatewayInterfaces.GatewayInterface[1].SubnetParticipation.AppendChild($ipRanges)
...下面从安斯加尔的意见,这是我在使用一个命名空间的尝试。(破)
[xml]$fragment = "<dummy xmlns:xsi='http://www.vmware.com/vcloud/v1.5'><IpRanges>$($ipRanges.InnerXml)</IpRanges></dummy>"
# $fragment.InnerXml ..returns..
# <dummy xmlns:xsi="http://www.vmware.com/vcloud/v1.5"><IpRanges><IpRange><StartAddress>185.39.247.98</StartAddress><EndAddress>185.39.247.98</EndAddress></IpRange></IpRanges></dummy>
# $body is the full XML Document I want to paste into
[xml]$xml = $body
$nsm = New-Object Xml.XmlNamespaceManager $xml.NameTable
$nsm.AddNamespace('xsi', $xml.NamespaceURI)
$node = $xml.ImportNode($fragment.DocumentElement.IpRanges, $true)
$subnetPart = $xml.SelectSingleNode("//IpAddress[text()='185.39.247.98']", $nsm)
$subnetPart
# returns nothing
“想不工作” 是不够的问题说明。你究竟试过了什么?那次尝试的结果究竟是什么? –
当然,我会在编辑中添加上面的内容,我不想发布大量问题,所以很抱歉,我最终放弃了这一部分。 –