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

Community site session details

Session Id : hv1UN4K/1ochPNJBB8kZFV
Power Automate - Power Automate Desktop
Answered

Replace all values in XML

Like (1) ShareShare
ReportReport
Posted on 2 Jul 2024 10:34:24 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?

Categories:
I have the same question (0)
  • BernatP Profile Picture
    on 02 Jul 2024 at 14:06:02
    Re: Replace all values in XML

    It worked! Thank you for the script.

  • Verified answer
    Deenuji_Loganathan_ Profile Picture
    6,206 Super User 2025 Season 2 on 02 Jul 2024 at 12:37:27
    Re: Replace all values in XML

    @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 🚀

  • NathanAlvares24 Profile Picture
    1,685 Moderator on 02 Jul 2024 at 12:32:01
    Re: Replace all values in XML

    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.

  • BernatP Profile Picture
    on 02 Jul 2024 at 11:46:22
    Re: Replace all values in XML
    <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

  • Deenuji_Loganathan_ Profile Picture
    6,206 Super User 2025 Season 2 on 02 Jul 2024 at 11:36:22
    Re: Replace all values in XML

    @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 🚀

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

MS.Ragavendar – Community Spotlight

We are honored to recognize Ragavendar Swaminatha Subramanian as our September…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 798 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 469 Moderator

#3
Power Apps 1919 Profile Picture

Power Apps 1919 333

Last 30 days Overall leaderboard
Loading complete