Modifying an existing node, whether it is an attribute or an element value, can be done by assigning a new value:
[XDocument]$xDocument = @" <?xml version="1.0"?> <items> <item name='Fridge'> <category>Appliancse</category> </item> <item name='Cooker'> <category>Appliances</category> </item> </items> "@ $xDocument.Element('items'). Elements('item'). Where( { $_.Attribute('name').Value -eq 'Fridge' } ). ForEach( { $_.Element('category').Value = 'Appliances' } )
Modifying the value of an attribute uses the same syntax:
[XDocument]$xDocument = @" <?xml version="1.0"?> <list name='letters'> <name>1</name> </list> "@ $xDocument.Element('list').Attribute('name').Value = 'numbers'
If the attribute does not exist, an error will be thrown:
PS> $xDocument.Element('list').Attribute('other').Value = 'numbers' The property 'Value' cannot be found on this object. Verify that the property exists and can be set. At line:1 char:1 + $xDocument.Element('list').Attribute('other').Value = 'numbers' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId :PropertyNotFound