Hello!
I have an xml file readed into a variable. From that text I want to replace all the values to 0 in the element <Min></Min>.
I tried establish a value from an XML element but it only replaces the first of them.
Also tried replacing text but don't know how to use something like <Min>*</Min> to <Min>0</Min>
Any idea how to do it?
It worked! Thank you for the script.
Please follow the simple approach:
Just replace your xml document path. PowerShell will do all the magic and return revised XML document.
Code(This is full power automate desktop flow code):
SET inputVariable TO $'''C:\\Boot\\TypeConfig.XML'''
@@copilotGeneratedAction: 'False'
Scripting.RunPowershellScript.RunPowershellScript Script: $'''# This code has been generated by AI. Original prompt:
# Read XML document from local drive which pass as input variable and set all min and max elements to 0 in an XML document using PowerShell
# Get the XML file path from the input variable
$xmlFilePath = \"%inputVariable%\"
# Load the XML document
[xml]$xml = Get-Content -Path $xmlFilePath
# Select all Min elements and set their value to 0
$minElements = $xml.SelectNodes(\"//Min\")
foreach ($minElement in $minElements) {
$minElement.InnerText = \"0\"
}
# Select all Max elements and set their value to 0
$maxElements = $xml.SelectNodes(\"//Max\")
foreach ($maxElement in $maxElements) {
$maxElement.InnerText = \"0\"
}
# Output the modified XML
$xml.OuterXml''' ScriptOutput=> XMLDocument ScriptError=> ScriptError
How to copy/paste the above code into your desktop flow?
Thanks,
Deenuji Loganathan 👩‍💻
Automation Evangelist 🤖
Follow me on LinkedIn 👥
-------------------------------------------------------------------------------------------------------------
If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀
Hi @BernatP !
I found a way to use Python script and PowerShell script for your preferred use case. Would you like to use this way?
Flow details:
I have this python script and xml file stored in a folder.
Python script:
What it is actually doing is that it gets all the Min values by xpath "//Min" and replace it to 0.
Make sure to install the "lxml" module for this to work. Use this command in your cmd (Administrator):
pip install lxml
XML data (I made an assumption of your xml as it had missing tags):
<TypeConfig>
<Keyword>1</Keyword>
<HeightALimit>
<Min>4.5</Min>
<Max>20</Max>
</HeightALimit>
<HeightB>
<Min>4</Min>
<Max>15</Max>
</HeightB>
<GLimit>
<Max>11.9</Max>
</GLimit>
<GLimitB>
<Min>0.5</Min>
<Max>5</Max>
</GLimitB>
<Subtype>
<SubtypeConfig>
<Keyword>1</Keyword>
<GLimit>
<Min>3.6</Min>
<Max>11.9</Max>
</GLimit>
<GLimitB>
<Min>0.5</Min>
<Max>5</Max>
</GLimitB>
</SubtypeConfig>
</Subtype>
</TypeConfig>
PowerShell script:
# Execute the Python script and capture its output
$output = & '%pythonExePath%' '%pythonScriptPath%'
# Output the modified XML
Write-Output $output
I stored the paths in my flow as seen in the very first pic.
Output:
<TypeConfig>
<Keyword>1</Keyword>
<HeightALimit>
<Min>0</Min>
<Max>20</Max>
</HeightALimit>
<HeightB>
<Min>0</Min>
<Max>15</Max>
</HeightB>
<GLimit>
<Max>11.9</Max>
</GLimit>
<GLimitB>
<Min>0</Min>
<Max>5</Max>
</GLimitB>
<Subtype>
<SubtypeConfig>
<Keyword>1</Keyword>
<GLimit>
<Min>0</Min>
<Max>11.9</Max>
</GLimit>
<GLimitB>
<Min>0</Min>
<Max>5</Max>
</GLimitB>
</SubtypeConfig>
</Subtype>
</TypeConfig>
Then with the help of this output, you can use any XML actions from PAD as this output acts like a XmlDocument.
If you want to re-write the file with this modified data then simply use "Write XML to file" action.
Before:
After:
This was just one XML action but you can use any of them.
Code:
SET pythonExePath TO $'''C:\\Python312\\python.exe'''
SET pythonScriptPath TO $'''C:\\Users\\Username\\testingpy.py'''
@@copilotGeneratedAction: 'False'
Scripting.RunPowershellScript.RunPowershellScript Script: $'''# Execute the Python script and capture its output
$output = & \'%pythonExePath%\' \'%pythonScriptPath%\'
# Output the modified XML
Write-Output $output''' ScriptOutput=> PowershellOutput ScriptError=> ScriptError
Text.Trim Text: PowershellOutput TrimOption: Text.TrimOption.Both TrimmedText=> PowershellOutput
XML.WriteXmlToFile.WriteToFileFormatted File: $'''C:\\Users\\Username\\testingminxml.xml''' Xml: PowershellOutput Encoding: XML.FileEncoding.DefaultEncoding Indentation: 2
XML.ExecuteXPathQuery.ExecuteXPath XmlDocument: PowershellOutput XPathQuery: $'''//Min''' XPathResults=> XPathResults
I am still trying to figure out a much simpler way using just XML actions. So I'll do some research then.
I hope this helps.
<TypeConfig>
<Keyword>1</Keyword>
<HeightALimit>
<Min>4.5</Min>
<Max>20</Max>
</HeightALimit>
<HeightB>
<Min>4</Min>
<Max>15</Max>
</HeightB>
<GLimit>
<Max>11.9</Max>
</GLimit>
<GLimitB>
<Min>0.5</Min>
<Max>5</Max>
</GLimitB>
<Subtype>
<SubtypeConfig>
<Keyword>1</Keyword>
<GLimit>
<Min>3.6</Min>
<Max>11.9</Max>
</GLimit>
<GLimitB>
<Min>0.5</Min>
<Max>5</Max>
</GLimitB>
</SubtypeConfig>
As you can see I got a lot of <Min> and <Max> elements. I want to set their content to 0.
For this I tried replacing text or replacing value in XML.
Please share screenshot of your workflow and sample xml after hiding all senstive information. we will try to assist you on the same.
Thanks,
Deenuji Loganathan 👩‍💻
Automation Evangelist 🤖
Follow me on LinkedIn 👥
-------------------------------------------------------------------------------------------------------------
If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀
Michael E. Gernaey
497
Super User 2025 Season 2
David_MA
436
Super User 2025 Season 2
Riyaz_riz11
244
Super User 2025 Season 2