简体   繁体   中英

Updating an xml attribute with boolean value using powershell

I have a sample XML as below:

<?xml version="1.0" encoding="utf-8"?>
<Searchable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" label="$RESX('Ebr.Crdm.Store.2_0','Item','Item')" xmlns="http://sysrepublic.com/Secure/4.0/DSL/SearchService">

  <Fields>
    <Field data-type="System.Int64" is-editable="true" id="item_transactionid" label="$RESX('Ebr.Crdm.Store.2_0','TransactionID','Transaction ID')" is-mandatory-display-field="false" is-hidden-display-field="false" is-virtual-date-time="false" is-display-field-only="false" use-utc-datetime="true" apply-user-timezone-offset="false" show-date="true" show-time="true" is-favourite="false" common-field="true">
      <FieldDescription primary-key="false" nullable="false" readonly="false" hidden="true" format="text" enable-required-validation="true" />
      <Operators>
        <Operator name="equal" />
        <Operator name="notequal" />
        <Operator name="greaterthan" />
        <Operator name="greaterthanequal" />
        <Operator name="lessthan" />
        <Operator name="lessthanequal" />
        <Operator name="between" />
        <Operator name="notbetween" />
        <Operator name="in" />
        <Operator name="notin" />
      </Operators>
      <LeftExpression>
        <Field data-type="System.Int64" common-field="true">
          <CollectionDescription collection="pos.CRDM_Item">
            <Relationship parent="pos.CRDM_Header">
              <RelatedField from="TransactionID" to="TransactionID" common-to-field="true" />
              <RelatedField from="TradingDay" to="TradingDay" common-to-field="true" />
            </Relationship>
          </CollectionDescription>
          <Description>TransactionID</Description>
        </Field>
      </LeftExpression>
    </Field>

 </Fields>
</Searchable>

Below is the powershell code that I tried:

$xmlFile = "C:\Users\rparpani\Desktop\test2.xml"
[xml]$xml = Get-Content $xmlFile -Raw

$nodes = $xml.SelectNodes("//Field[@id = 'item_transactionid']")
foreach ($node in $nodes)
{
$node.is-hidden-display-field = 'True'


}

xml.Save('C:\Users\rparpani\Desktop\test2.xml')

It appears that there is a different way to update a Boolean attribute. I'm trying to change the is-hidden-display-field from false to true. Can someone please suggest what is wrong with my code

Your XML document uses namespaces , so you cannot locate your Field elements unless you pass a namespace-manager instance to the .SelectNodes() call. Without that, no nodes match, and your loop is never entered.

However, PowerShell's convenient adaption of the XML DOM via dot notation is namespace-agnostic, which enables a more convenient (albeit slower) solution:

$xml.Searchable.Fields.Field | 
  where id -eq 'item_transactionid' |
   foreach { $_.'is-hidden-display-field' = 'true' }

Also note how attribute name is-hidden-display-field must be quoted in order to be used as a PowerShell property name.

For background information, see this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM