I'm really struggling to manipulate some XML in PowerShell that I need to send back to a web service as a body. Could anyone help me get the XML laid out the way it needs to be please?
<?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>
needs to become:
<?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>
So far I've been able to create new XML Nodes/Elements for the new content, but I cannot get it to be inserted at the correct place. I can get the AppendChild()
method to work, but it puts the content after the <UseForDefaultRoute>
section - not before.
I've tried to do InsertBefore()
and InsertAfter()
, but it just doesn't want to work.
Finally, when I do the AppendChild()
approach, I also get a bit of extra text I wasn't expecting, something about xmlns?
<IpRanges xmlns=""><IpRange><StartAddress>1.2.3.5</StartAddress><EndAddress>1.2.3.5</EndAddress></IpRange></IpRanges>
This is what I managed to put together, bear in mind that it's broken :(
# 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)
...following advice from Ansgar, this is my attempt at using a namespace. (broken)
[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