0

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

1
  • 1
    Hi, George, what version of PowerShell? Commented Jul 22, 2009 at 12:06

2 Answers 2

3

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

use

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.