views:

182

answers:

2

Hello everyone,

I have the following XML file and I want to print out the baseAddress value, then change the value and write update to the same XML file.

My problem is I am using the following scripts in PowerShell to manipulate, and seems the related value could not be retrieved. I think the reason may be there is a sign '.' in the element name "system.serviceModel", which PowerShell thinks I want to retrieve serviceModel sub-element under system? Any ideas how to retrieve the correct value for baseAddress?

$FooConfig = [xml](get-content .\Foo.exe.config -ErrorAction:stop)
FooConfig.configuration.system.serviceModel.services.service.host.baseAddress

<configuration>
  <system.serviceModel>
    <services>
      <service name="FooImpl" behaviorConfiguration="mexServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9090/Foo" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

thanks in advance, George

+3  A: 

If you put quotes around the element name with a "." you can get it.

use

$FooConfig.configuration."system.serviceModel".services.service.host.baseAddress

Andy Schneider
Thanks Andy, I have got the solution, $FooConfig.configuration."system.serviceModel".services.service.host.baseAddresses.add.baseaddress
George2
+1  A: 

In some cases, it is easier to just use XPATH and the XML API e.g.:

PS> $FooConfig.SelectSingleNode('//add[@baseAddress]').baseAddress = 'foo'
PS> $FooConfig.SelectSingleNode('//add[@baseAddress]').baseAddress
foo

versus:

PS> $xml.configuration.'system.serviceModel'.services.service.host.baseaddresses.add.baseaddress = 'foo'
PS> $xml.configuration.'system.serviceModel'.services.service.host.baseaddresses.add.baseaddress
foo