I am trying to access and change the particular attribute from XML tag
XML:
<office> <staff branch="Hanover" Type="sales"> <employee> <Name>Tobias Weltner</Name> <function>management</function> <age>39</age> </employee> <employee> <Name>Cofi Heidecke</Name> <function>security</function> <age>4</age> </employee> </staff> <staff branch="London" Type="Technology"> <employee> <Name>XXXX</Name> <function>gement</function> <age>39</age>
From the above example I want to print branch attribute and then want to change it with one value such as New York in all the whole XML and using below code to do that
$xml=New-Object XML $xml.Load("C:\FE6Work.xml") $node=$xml.SelectNodes("/office/staff") write-output $node.branch $node.branch="New York"
But get an error stating can't find the element.
Can someone please help?
To update the specific XML node using PowerShell, we first need to select that node with the attribute with SelectSingleNode() method. We have below the XML file from the link stored in SampleXml. XML on C:\Temp location. The above commands will load the XML file and select node with attribute value 'bk102'.
The way to change the value of an attribute, is to change its text value. This can be done using the setAttribute() method or setting the nodeValue property of the attribute node.
Independently, you can use the type-native . InnerText property to replace an element's current child nodes, if any, with a text node, as shown in Mathias' answer.
In the above example, XML element is text, the category is the attribute name and message is the attribute value, Attribute name and its value always appear in pair. The attribute name is used without any quotation but attribute value is used in single ( ' ' ) or double quotation ( ” ” ).
# Change the value of attribute "b" to "pear". $success = $xml. UpdateAttribute ( "b", "pear" ) # Because no attribute named "c" exists, UpdateAttribute # will create it: $success = $xml. UpdateAttribute ( "c", "orange" ) # If an attribute's value is an integer, the AddToAttribute # method may be called to update it by adding an integer value.
To update the specific XML node using PowerShell, we first need to select that node with the attribute with SelectSingleNode () method. We have below the XML file from the link stored in SampleXml.XML on C:\Temp location. In this example, we are going to update Autor and Genre properties of the Book having attribute Id = ‘bk102’
The Get-XmlElementsTextValue function is pretty straight forward; return the value if it exists, otherwise return null. The Set-XmlElementsTextValue is a little more involved because if the element does not exist already, we need to create the new element and attach it as a child to the parent element.
How to change files and folders attributes using PowerShell? There are multiple files and folders attribute supported by the Windows Operating System. To check which attributes that files and folders support use DOS command attrib /? You can see the attributes listed like Read-Only, Archive, etc. You can set the attribute using PowerShell.
Try the following:
$nodes = $xml.SelectNodes("/office/staff"); foreach($node in $nodes) { $node.SetAttribute("branch", "New York"); }
This will iterate through all nodes returned by SelectNodes() and modify each one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With