Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the value of XML Element attribute using PowerShell?

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?

like image 409
user3759904 Avatar asked Jul 10 '14 14:07

user3759904


People also ask

How do I update XML attributes in PowerShell?

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'.

How do you change a value in XML?

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.

How do I replace text in XML in PowerShell?

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.

How do I include in XML attribute value?

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 ( ” ” ).

How do I change the value of an attribute in XML?

# 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.

How to update specific XML node using PowerShell?

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’

How do I get the value of an XML element?

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?

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.


1 Answers

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.

like image 142
PeterK Avatar answered Oct 11 '22 15:10

PeterK