xmlpowershellselect-xml

Trying to get XML element


Here is a piece of XML file from which I want to extract the IP Address (here 172.31.24.8) using PowerShell code:

<?xml version="1.0"?>
<Configuration xmlns="http://www.tandberg.com/XML/CUIL/2.0" product="Cisco Codec" version="TC7.3.2.14ad7cc" apiVersion="2">
  <SIP item="1">
    <Profile item="1" maxOccurrence="1">
      <Proxy item="1" maxOccurrence="4">
        <Address item="1" valueSpaceRef="/Valuespace/STR_0_255_NoFilt">172.31.24.8</Address>
        <Discovery item="1" valueSpaceRef="/Valuespace/TTPAR_AutoManual">Manual</Discovery>
      </Proxy>
    </Profile>
  </SIP>
</Configuration>

I tried to use Select-Xml as described in other StackOverflow examples, but unsuccessfully so far.

What's the simplest way to achieve this properly?


Solution

  • I'm not familiar with Powershell, so I cannot help you with the Powershell code, but I think Select-Xml works with XPath expressions.

    Given your XML document, and given that you can somehow register a default namespace, the following XPath expression would work:

    /Configuration/SIP/Profile/Proxy/Address/text()
    

    If you cannot register a default namespace, perhaps you can register a prefix and a namespace, e.g.

    /tb:Configuration/tb:SIP/tb:Profile/tb:Proxy/tb:Address/text()
    

    where tb must correspond to the namespace http://www.tandberg.com/XML/CUIL/2.0. Here is a link that explains how to declare namespaces.


    If Select-Xml cannot deal with namespaces, use

    /*[local-name() = 'Configuration']/*[local-name() = 'SIP']/*[local-name() = 'Profile']/*[local-name() = 'Proxy']/*[local-name() = 'Address']/text()
    

    and the only result of all those path expressions will be

    172.31.24.8