I have an XML document $books that uses namespaces, and I've tried to create an XML namespacemanager by using something like $ns = New-Object System.Xml.XmlNamespaceManager($books.NameTable). I'm not quite sure what I'm doing wrong after that though. I'm trying to use SelectSingleNode() to search the XML document for specific nodes I need, but all my XPath queries return null.
Here's a bit of the xml document:
<ernm:NewReleaseMessage xmlns:ernm="http://ddex.net/xml/ern/341" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" LanguageAndScriptCode="en" MessageSchemaVersionId="/ern/341" xs:schemaLocation="http://ddex.net/xml/ern/341 http://ddex.net/xml/ern/341/release-notification.xsd">
<MessageHeader>
<MessageId>123123</MessageId>
<MessageRecipient>
<PartyId>567567</PartyId>
<PartyName>
<FullName>John Smith</FullName>
</PartyName>
</MessageRecipient>
</MessageHeader>
</ernm:NewReleaseMessage>
Also, here's a bit of what I've tried so far in order to get SelectSingleNode() working correctly:
[xml]$books = Get-Content xmlpath.xml
$ns = New-Object System.Xml.XmlNamespaceManager($books.NameTable)
$ns.AddNamespace("ns", $books.DocumentElement.NamespaceURI)
$books.SelectSingleNode("//newreleasemessage", $ns) #returns null
$books.SelectSingleNode("//ns:newreleasemessage", $ns) #returns null
$books.SelectSingleNode("//ernm:newreleasemessage", $ns) #returns null
$books.SelectSingleNode("//xmlns:newreleasemessage", $ns) #returns null
$books.SelectSingleNode("//xml:newreleasemessage", $ns) #returns null
I've also tried other nodes than newreleasemessage and it still comes back blank, I just wanted to provide some examples of what I've tried. What's the correct way of using namespaces here?