Read XML file which contains namespace PowerShell Read XML file which contains namespace PowerShell powershell powershell

Read XML file which contains namespace PowerShell


When you use the Select-Xml cmdlet with the -XPath parameter, you also need to specify the namespace. See this article for more information:

http://huddledmasses.org/xpath-and-namespaces-in-powershell/

Here is a complete, working example

$XmlDocument = [xml]@'<?xml version="1.0" encoding="utf-8"?><ServiceDefinition name="test" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2012-10.1.8">  <WebRole name="test" vmsize="Small">    <Sites>      <Site name="Web">        <Bindings>          <Binding name="Endpoint1" endpointName="HttpsEndpoint" />          <Binding name="Endpoint2" endpointName="HttpEndpoint" />        </Bindings>      </Site>    </Sites>    <Endpoints>      <InputEndpoint name="HttpsEndpoint" protocol="https" port="443" certificate="Certificate1" />      <InputEndpoint name="HttpEndpoint" protocol="http" port="80" />    </Endpoints>    <Imports>      <Import moduleName="Diagnostics" />      <Import moduleName="RemoteAccess" />      <Import moduleName="RemoteForwarder" />    </Imports>    <Certificates>      <Certificate name="Certificate1" storeLocation="LocalMachine" storeName="My" />    </Certificates>    <ConfigurationSettings>      <Setting name="LogLevel" />    </ConfigurationSettings>  </WebRole></ServiceDefinition>'@;$XmlNamespace = @{ azure = 'http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition'; };Select-Xml -Xml $XmlDocument -XPath '/azure:ServiceDefinition/azure:WebRole/@vmsize' -Namespace $XmlNamespace;