How to Convert XML to string in Power Automate?

In this Power Automate tutorial, we will see how to convert XML to string in Power Automate.

XML is an abbreviation for “eXtensible Markup Language.” It is a markup language used to define and organize data in a human-readable and machine-readable format. XML is meant to be self-descriptive, which means that data items are specified using custom tags, making it simple for humans to understand the purpose of the content.

For example, we will take the below XML, and we will perform the tasks:

  • Get the person’s names
  • Get all the person nodes and retrieve the person’s details
  • Get the person’s details based on the condition
<root>
  <person>
    <name>John Doe</name>
    <age>30</age>
    <email>john.doe@example.com</email>
  </person>
  <person>
    <name>Jane Smith</name>
    <age>25</age>
    <email>jane.smith@example.com</email>
  </person>
  <person>
    <name>Michael Johnson</name>
    <age>35</age>
    <email>michael.johnson@example.com</email>
  </person>
</root>

We will parse the XML using Xpath () to do this task. As a result, XPath is a commonly used query language for extracting data (nodes) from an XML document.

The syntax of Xpath

xpath(<XML data>, <xpath expression>)

How to convert XML to string using Power Automate

Here we will see how to convert XML to a string by parsing the XML using Power Automate.

We will do below 3 examples:

  • Get the person’s names
  • Get all the person nodes and retrieve the person’s details
  • Get the person’s details based on the condition

Get the person’s names

We will pass the above XML and get the names of the person by parsing the XML using Power Automate. To do this, follow the below steps.

Step-1:

  • Login to Power Automate, click on +Create -> select Instant Cloud Flow.
microsoft flow convert xml to string
  • Then provide the flow name, and select the Manually trigger a flow. Then click on Create.
Power Automate convert xml to string
  • Now, you can see the Manually trigger a flow action is the flow page.
Convert XML to string in Power Automate

Step 2: Next, click on the +New step -> select Compose action. Then provide the below information:

  • Inputs: In inputs, provide the below XML
<root>
  <person>
    <name>John Doe</name>
    <age>30</age>
    <email>john.doe@example.com</email>
  </person>
  <person>
    <name>Jane Smith</name>
    <age>25</age>
    <email>jane.smith@example.com</email>
  </person>
  <person>
    <name>Michael Johnson</name>
    <age>35</age>
    <email>michael.johnson@example.com</email>
  </person>
</root>
Convert XML to string in Power Automate

Step 3: Next, we will parse the XML and get the person’s names. After parsing the XML, it returns the output in base64 to get it in the string; we will use the text().

So, click on the +New step and select compose actions. Then provide the below information:

  • Inputs: Provide the following expressions:
xpath(xml(outputs('Compose_-XML')), '//person/name/text()')
Using Power Automate convert xml to string

Step 4: To see the output, we will send an email containing person names using Power Automate.

So, click the +New step -> select Send an email (V2) action. Then provide the below information:

  • To: Provide whom you will send an email
  • Subject: Provide the subject of an email
  • Body: Provide the body of an email.
@{outputs('Compose_-names')[0]}
@{outputs('Compose_-names')[1]}
@{outputs('Compose_-names')[2]}
Convert XML to string in Power Automate

Step 5: To run the flow manually, click on Test-> select Manually -> click on the Test button. Next, click on the Test. Now you can see the flow run successfully. You can see the person’s name is sent to an email.

convert xml to string Power automate

Get all the person nodes and retrieve the person’s details

Here we will see how to get all the personal details from the XML content by parsing it using Power Automate.

Step-1: Login to Power Automate and click +Create -> select Instant Cloud Flow.

microsoft flow convert xml to string
  • Then provide the flow name, and select the Manually trigger a flow. Then click on Create.
Power Automate convert xml to string
  • Now, you can see the Manually trigger a flow action is the flow page.
Microsoft Power Automate convert xml to string

Step 2: Next, click the +New step -> select Compose action. Then provide the below information:

  • Inputs: In inputs, provide the below XML
<root>
  <person>
    <name>John Doe</name>
    <age>30</age>
    <email>john.doe@example.com</email>
  </person>
  <person>
    <name>Jane Smith</name>
    <age>25</age>
    <email>jane.smith@example.com</email>
  </person>
  <person>
    <name>Michael Johnson</name>
    <age>35</age>
    <email>michael.johnson@example.com</email>
  </person>
</root>
Using Microsoft Power Automate convert xml to string

Step 3: Now, we will parse the above XML and get the person nodes. So, click on the +New step -> select Compose action.

Then provide the below information:

  • Inputs: Provide the below expressions:
xpath(xml(outputs('Compose_-XML')), '/root/person')
convert xml to string Microsoft Power automate

Step 4: Next, initialize the three variables, so click on the +New step -> select Initialize variable action. Then, provide the below information:

Variable nameType
nameArray
ageArray
emailArray
convert xml to string using Microsoft Power automate

Step 5: Next, we will loop through person nodes to get the details, so click on the +New step -> select Apply to each action. Provide the below information:

  • Select an output from previous steps: Select the output from the dynamic content.

Next, we will Add an action -> select Compose action, then provide the below information:

  • Inputs: Provide the below expression
@{items('Apply_to_each')?['$content']}

After that, click on Add an action -> select Compose action to convert base64 to string, then provide the below information:

  • Inputs: Provide the below expression:
base64ToString(outputs('Compose_4'))
convert xml to string using Power automate

Step 6: Now we will get the name, age, and email of the Person. So, click on the Add an action, and select Append to array variable action. Then provide the below information:

Variable nameValue
namesjoin(xpath(xml(outputs(‘Compose_5’)), ‘/person/name/text()’),”)
agejoin(xpath(xml(outputs(‘Compose_5’)), ‘/person/age/text()’),”)
emailsjoin(xpath(xml(outputs(‘Compose_5’)), ‘/person/email/text()’),”)
How to convert xml to string using Power automate

Step 7: Now, we will see the output, so click on the +New step -> select ‘Send an email (V2)action. Then provide the below information:

  • To: Provide whom you will send an email
  • Subject: Provide the subject of an email
  • Body: Provide the body of an email like the one below.
Person 1
name: @{variables('names')[0]}
age: @{variables('age')[0]}
email: @{variables('email')[0]}

Person 2
name: @{variables('names')[1]}
age: @{variables('age')[1]}
email: @{variables('email')[1]}

Person 3
name: @{variables('names')[2]}
age: @{variables('age')[2]}
email: @{variables('email')[2]}
How to convert xml to string using Microsoft Power automate

Step 8: To run the flow manually, click on Test-> select Manually -> click the Test button. Next, click on the Test. Now you can see the flow run successfully. You can see the person’s details are sent to an email.

How to convert xml to string using Microsoft  flow

Get the person’s details based on the condition

Here we will parse the XML and get the details of the person whose age is greater than 28 using Power Automate.

Step-1: Log in to Power Automate and click +Create -> select Instant Cloud Flow.

microsoft flow convert xml to string
  • Then provide the flow name, and select the Manually trigger a flow. Then click on Create.
Power Automate convert xml to string
  • Now, you can see the Manually trigger a flow action is the flow page.
Microsoft Power Automate convert xml to string

Step 2: Next, click on the +New step -> select Compose action. Then provide the below information:

  • Inputs: In inputs, provide the below XML code.
<root>
  <person>
    <name>John Doe</name>
    <age>30</age>
    <email>john.doe@example.com</email>
  </person>
  <person>
    <name>Jane Smith</name>
    <age>25</age>
    <email>jane.smith@example.com</email>
  </person>
  <person>
    <name>Michael Johnson</name>
    <age>35</age>
    <email>michael.johnson@example.com</email>
  </person>
</root>
Using Microsoft Power Automate convert xml to string

Step 3: Now, we will parse the above XML and get the person nodes. So, click on the +New step -> select Compose action.

Then provide the below information:

  • Inputs: Provide the below expressions:
xpath(xml(outputs('Compose_-XML')),'/root/person[age>28]')
How to convert xml to string Microsoft  flow

Step 4: Next, initialize the three variables, so, click on the +New step -> select Initialize variable action. Then, provide the below information:

Variable nameType
Person detailsArray
How to convert xml to string Microsoft flow

Step 5: Next, we will loop through person nodes to get the details, so click on the +New step -> select Apply to each action. Provide the below information:

  • Select an output from previous steps: Select the output, from the dynamic content.

Next, we will Add an action -> select Compose action, then provide the below information:

  • Inputs: Provide the below expression
@{items('Apply_to_each')?['$content']}

After that, click on Add an action -> select Compose action to convert base64 to string, then provide the below information:

  • Inputs: Provide the below expression:
base64ToString(outputs('Compose_4'))
convert xml to string using Power automate

Step 6: Now we will get the name, age, and email of the Person. So, click on the Add an action, and select Append to array variable action. Then provide the below information:

Variable nameValue
Person details{
“Name”:@{join(xpath(xml(outputs(‘Compose_5’)), ‘/person/name/text()’),”)},
“Age”: @{join(xpath(xml(outputs(‘Compose_5’)), ‘/person/age/text()’),”)},
“Email”:@{join(xpath(xml(outputs(‘Compose_5’)), ‘/person/email/text()’),”)}
}
How to convert xml to string Microsoft Power Automate

Step 7: Next, we will create an html table for personal details, click on the +New step -> select ‘Create Html table’ action. Then provide the below information:

  • From: Provide the variable array from dynamic content.
convert xml data to string Microsoft Power Automate

Step 8: Now, we will see the output, so, click on the +New step -> select ‘Send an email (V2)action. Then provide the below information:

  • To: Provide whom you will send an email
  • Subject: Provide the subject of an email
  • Body: Provide the output from the dynamic content.
parse xml to string Microsoft Power Automate

Step 9: To run the flow manually, click on Test-> select Manually -> click on the Test button. Next, click on the Test. Now you can see the flow run successfully. You can see the person’s details are sent to an email.

convert xml to string in Power Automate

Conclusion

In this Power Automate tutorial, we saw how to convert XML to string in Power Automate.

You may like the following tutorials:

>