Ryan Jensen
Ryan Jensen

Reputation: 101

Error with Powershell "Cannot set "Attribute" because only strings can be used as values to set XmlNode properties

I'm having an issue with assigning a value to an SOAP call that's an XML. The value is defined in a variable, but Powershell keeps returning this error:

"Cannot set "Attribute" because only strings can be used as values to set XmlNode properties"

My variable is defined as a string: [string]$AvidDisplayNameMinusTransfer

Here's the SOAP Call with the variable:

$soap = [xml]@'

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://avid.com/interplay/ws/assets/types">
   <soapenv:Header>
      <typ:UserCredentials>
         <typ:Username>***</typ:Username>
         <typ:Password>***</typ:Password>
      </typ:UserCredentials>
   </soapenv:Header>
   <soapenv:Body>
      <typ:Search>
         <typ:InterplayPathURI>interplay://AvidEng103/LTW</typ:InterplayPathURI>
         <typ:SearchGroup Operator="AND">
            <!--Zero or more repetitions:-->
            <typ:AttributeCondition Condition="EQUALS">
               <typ:Attribute Group="USER" Name="Display Name"></typ:Attribute>
            </typ:AttributeCondition>
         </typ:SearchGroup>
         <typ:ReturnAttributes>
            <typ:Attribute Group="SYSTEM" Name="MOB ID"></typ:Attribute>
         </typ:ReturnAttributes>
      </typ:Search>
   </soapenv:Body>
</soapenv:Envelope>

'@

$soap.Envelope.Body.Search.SearchGroup.AttributeCondition.Attribute = $AvidDisplayNameMinusTransfer

Let me know if anyone needs my entire code to give better context.

Upvotes: 7

Views: 7891

Answers (2)

Iconiu
Iconiu

Reputation: 367

This error is silly in my opinion because the variable is a string. However, if you put quotes around the variable name, it works. Using innerhtml requires editing the entire node above as the attribute doesn't have an innerhtml property. The attribute itself has a get/set method so should work.

$soap.Envelope.Body.Search.SearchGroup.AttributeCondition.Attribute = "$AvidDisplayNameMinusTransfer"

Upvotes: 3

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200453

Use the innerText property to assign a string as the content of the tag:

$soap.Envelope.Body.Search.SearchGroup.AttributeCondition.Attribute.innerText = $AvidDisplayNameMinusTransfer

Result:

PS C:\> $soap.Envelope.Body.Search.SearchGroup.AttributeCondition.Attribute

Group         Name               #text
-----         ----               -----
USER          Display Name       foo

Upvotes: 8

Related Questions