Reputation: 13
I am trying to select and remove a single node from an XML document. Sample code and XML below:
[xml]$xml = Get-Content MyXml.xml
$xml.MigrationTable.Mapping[1].SelectSingleNode("DestinationSameAsSource")
This currently returns nothing. This answer shows a C# example for including the namespace when calling the SelectSingleNode()
method.
How can I include the namespace with SelectSingleNode()
in PowerShell?
XML:
<?xml version="1.0" encoding="UTF-16"?>
<MigrationTable xmlns="http://microsoft.com/GroupPolicy/GPOOperations/MigrationTable" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Mapping>
<Type>LocalGroup</Type>
<Source>[email protected]</Source>
<Destination>[email protected]</Destination>
</Mapping>
<Mapping>
<Type>Unknown</Type>
<Source>Network Service</Source>
<DestinationSameAsSource/>
</Mapping>
<Mapping>
<Type>Unknown</Type>
<Source>Local Service</Source>
<DestinationSameAsSource/>
</Mapping>
</MigrationTable>
Upvotes: 0
Views: 1386
Reputation: 24071
The Powershell syntax for NameTable handling is pretty similar to C#. After loading the data, create a NamespaceManager based on the XML document.
To select elements, a dummy namespace prefix needs to be used, in this sample x
is added and used in Xpath. Like so,
[xml]$xml = Get-Content MyXml.xml
$nsmgr = new-object Xml.XmlNamespaceManager($xml.NameTable)
$nsmgr.AddNameSpace("x", "http://microsoft.com/GroupPolicy/GPOOperations/MigrationTable")
$xml.MigrationTable.Mapping[1].SelectSingleNode("x:DestinationSameAsSource", $nsmgr)
Upvotes: 1