file - Powershell - read and exchange node value within XML -
I would like to read the following nodes and set a new value. Due to two existing single nodes with different ID, I am not able to change all the values.
Here's an example: XML Source:
& lt; Root & gt; & Lt; Node1 & gt; & Lt; Children & gt; & Lt; Children & gt; & Lt; ID & gt; 1 & lt; / Id & gt; & Lt; Targetvalue & gt; 9999 & lt; / Targetvalue & gt; & Lt; / Children & gt; & Lt; Children & gt; & Lt; ID & gt; 2 & lt; / Id & gt; & Lt; Targetvalue & gt; 9999 & lt; / Targetvalue & gt; & Lt; / Children & gt; & Lt; / Children & gt; & Lt; / Node1 & gt; & Lt; / Root & gt; If (($ myXML.root.node1.child.baby | where-item {$ _. Id -q '1'- and $ _ targetvalue -eq' 9999 '}) - A $ tap {"test"}
I tried it through the content already received, but due to two IDs I am not able to modify at least one value. Can anyone please help?
I would say: Use the right tool for the job. It is your first choice as you are working with XML - Select-Xml
(and XPath):
$ node = Select-Xml-Path path \ To \ Original .xml -XPath "// baby [id = '1']" $ node.Node.targetvalue = '444' $ node.Node.OwnerDocument.Save ('c: \ temp \ modified.xml')
Comments
Post a Comment