【问题标题】:Error with Powershell "Cannot set "Attribute" because only strings can be used as values to set XmlNode propertiesPowershell 出错“无法设置”属性”,因为只有字符串可以用作设置 XmlNode 属性的值
【发布时间】:2015-09-14 21:14:19
【问题描述】:

我在为 XML 形式的 SOAP 调用分配值时遇到问题。该值在变量中定义,但 Powershell 不断返回此错误:

"不能设置"属性",因为只有字符串可以用作值 设置 XmlNode 属性”

我的变量定义为字符串:[string]$AvidDisplayNameMinusTransfer

这是带有变量的 SOAP 调用:

$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

如果有人需要我的整个代码来提供更好的上下文,请告诉我。

【问题讨论】:

    标签: powershell soap


    【解决方案1】:

    使用innerText 属性分配一个字符串作为标签的内容:

    $soap.Envelope.Body.Search.SearchGroup.AttributeCondition.Attribute<b>.innerText</b> = $AvidDisplayNameMinusTransfer

    结果:

    PS C:\> $soap.Envelope.Body.Search.SearchGroup.AttributeCondition.Attribute
    
    Group         Name               #text
    -----         ----               -----
    USER          Display Name       foo

    【讨论】:

      【解决方案2】:

      我认为这个错误很愚蠢,因为变量是一个字符串。但是,如果您在变量名周围加上引号,它就可以工作。使用 innerhtml 需要编辑上面的整个节点,因为该属性没有 innerhtml 属性。属性本身有一个 get/set 方法,所以应该可以工作。

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

      【讨论】:

        猜你喜欢
        • 2018-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-29
        • 2014-02-08
        • 1970-01-01
        • 2011-08-26
        相关资源
        最近更新 更多