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.
- Then provide the flow name, and select the Manually trigger a flow. Then click on Create.
- Now, you can see the Manually trigger a flow action is the flow page.
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>
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()')
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]}
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.
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.
- Then provide the flow name, and select the Manually trigger a flow. Then click on Create.
- Now, you can see the Manually trigger a flow action is the flow page.
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>
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')
Step 4: Next, initialize the three variables, so click on the +New step -> select Initialize variable action. Then, provide the below information:
Variable name | Type |
---|---|
name | Array |
age | Array |
Array |
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'))
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 name | Value |
---|---|
names | join(xpath(xml(outputs(‘Compose_5’)), ‘/person/name/text()’),”) |
age | join(xpath(xml(outputs(‘Compose_5’)), ‘/person/age/text()’),”) |
emails | join(xpath(xml(outputs(‘Compose_5’)), ‘/person/email/text()’),”) |
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]}
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.
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.
- Then provide the flow name, and select the Manually trigger a flow. Then click on Create.
- Now, you can see the Manually trigger a flow action is the flow page.
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>
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]')
Step 4: Next, initialize the three variables, so, click on the +New step -> select Initialize variable action. Then, provide the below information:
Variable name | Type |
---|---|
Person details | Array |
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'))
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 name | Value |
---|---|
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()’),”)} } |
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.
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.
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.
Conclusion
In this Power Automate tutorial, we saw how to convert XML to string in Power Automate.
You may like the following tutorials:
- Convert XML to JSON using Power Automate
- How to loop through XML data and insert into Excel using Power Automate?
- How to Convert XML to SharePoint List using Power Automate?
- How to Convert an array to a string using Power Automate?
- How to Convert an array to a string using Power Automate?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com