none
add xml attribute to an element using powershell RRS feed

  • Question

  • Hi,

    I am trying to modify an xml config using powershell. I am familiar with modifying already set values. And I am familiar with creating an element, setting key/value attributes, and appending that element. What I am struggling to do is add an attribute to an existing element.

    For example: say I want to add an attribute called bindingConfiguration with value AppBinding to my endpoint element

    from:
    <configuration>
     <system.serviceModel>
      <services>
       <service name="ServiceName">
        <endpoint binding="basicHttpBinding">
        </endpoint>
        <host>
         <baseAddresses>
          <add baseAddress="localhost:81" />
         </baseAddresses>
        </host>
      </service>
     </system.serviceModel>
    </configuration>

    to:
    <configuration>
     <system.serviceModel>
      <services>
       <service name="ServiceName">
        <endpoint binding="basicHttpBinding" bindingConfiguration="AppBinding">
        </endpoint>
        <host>
         <baseAddresses>
          <add baseAddress="localhost:81" />
         </baseAddresses>
        </host>
      </service>
     </system.serviceModel>
    </configuration>

    Thanks,

    Jason


    Jason

    Wednesday, October 14, 2015 5:35 PM

Answers

  • figured it out:

    $xml=[xml](get-content $config)

    $xml.configuration.'system.serviceModel'.services.service.endpoint.setattribute("bindingConfiguration","AppBinding")

    Thanks,

    Jason


    Jason

    Wednesday, October 14, 2015 5:53 PM