web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Replace all values in XML
Power Automate
Unanswered

Replace all values in XML

(1) ShareShare
ReportReport
Posted on by

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?

I have the same question (0)
  • Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    @BernatP 

     

    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 🚀

  • BernatP Profile Picture
    on at
    <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.

    BernatP_0-1719920579252.png

  • NathanAlvares24 Profile Picture
    1,714 Moderator on at

    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:

    NathanAlvares24_0-1719922702124.png

     

    I have this python script and xml file stored in a folder.

    NathanAlvares24_1-1719922773779.png

     

    Python script:

    NathanAlvares24_2-1719922822630.png

    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:

    NathanAlvares24_3-1719923059624.png

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

    NathanAlvares24_4-1719923171525.png

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

    NathanAlvares24_7-1719923378809.png

     

    Before:

    NathanAlvares24_5-1719923334641.png

     

    After:

    NathanAlvares24_6-1719923350678.png

     

    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.

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,250 Super User 2025 Season 2 on at

    @BernatP 

     

    Please follow the simple approach:

     

    Just replace  your xml document path. PowerShell will do all the magic and return revised XML document.

     

    Deenuji_0-1719923718883.png

     

     

    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?

    Deenuji_1-1719923815165.gif

     


    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 🚀

  • BernatP Profile Picture
    on at

    It worked! Thank you for the script.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 523 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 406 Moderator

#3
abm abm Profile Picture

abm abm 245 Most Valuable Professional

Last 30 days Overall leaderboard