Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell set XML value #text

I try to set the value of an element. It it is not empty in my XML, it works, but if it is empty, is doesn't work.

My XML:

...
<Field IDREF="934d80f1" Name="FirstName">works</Field>
<Field IDREF="936845ba" Name="LastName"></Field>
...

If I write the contents with

$record.BusinessObject.FieldList.Field | Where-Object { $_.Name -eq $field }

it returns

IDREF           Name                        #text
-----           ----                        -----
934d80f1        FirstName                   works

And in the second row the "virtual" Attribute #text is missing:

IDREF           Name
-----           ----
936845ba        LastName

with the PowerShell code

record.BusinessObject.FieldList.Field | Where-Object { $_.Name -eq $field } | ForEach-Object { $_.'#text' = $state }

I want to set the value >VALUE< of the selected element. It doesn't work if the value is empty...

How can I do this?

like image 795
Dominik Avatar asked Sep 27 '26 22:09

Dominik


1 Answers

Try using the $_.InnerXml property, and it will set the value on the required field.

$record.BusinessObject.FieldList.Field | Where-Object {
  $_.Name -eq $field
} | ForEach-Object {
  $_.InnerXml = $state
}
like image 68
Musaab Al-Okaidi Avatar answered Sep 30 '26 12:09

Musaab Al-Okaidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!