How to Convert XML to CSV using Power Automate?

In this Power Automate tutorial, we will see how to convert xml to csv using Power Automate.

For example, we will take the below XML data, and then we will convert it to the csv table, and we will send it with an email attachment.

<root>
  <person>
    <name>John Doe</name>
    <age>30</age>
    <email>[email protected]</email>
  </person>
  <person>
    <name>Jane Smith</name>
    <age>25</age>
    <email>[email protected]</email>
  </person>
  <person>
    <name>Michael Johnson</name>
    <age>35</age>
    <email>michael.j[email protected]</email>
  </person>
</root>

For this, we will create an Instant cloud flow that will trigger manually. Then we will parse the above XML and create an array of data. By using this array of data, we will create a csv table. At last, we will send an email to the recipients, with the attachment; the content will be the output of csv table action

Power automate xml to csv

Convert XML to CSV file using Power Automate

Here we will create an Instant cloud flow and convert xml to csv file using Power Automate.

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

convert xml to csv in Microsoft Power Automate

Then provide the flow name, and select the Manually trigger a flow. Then click on Create.

xml to csv Power automate

Now, you can see the Manually trigger a flow action is the flow page.

Power automate xml to csv file

Step 2: Next, click 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>[email protected]</email>
  </person>
  <person>
    <name>Jane Smith</name>
    <age>25</age>
    <email>[email protected]</email>
  </person>
  <person>
    <name>Michael Johnson</name>
    <age>35</age>
    <email>michael.j[email protected]</email>
  </person>
</root>
Power automate convert xml to csv file

Step 3: Next, we will parse the XML and get all the person nodes. After parsing the XML it returns the output in base64 to get it in a 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')), '/root/person')
Microsoft flow convert xml to csv file

Step 4: Next, we will initialize a variable, and later we will use to create an array of person details. So, click on the New step -> select Initialize variable action. Then provide the below information:

  • Name: Provide the variable name.
  • Type: Select the type as Array from the dropdown.
Microsoft Power automate convert xml to csv file

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'))
Microsoft flow convert xml to csv file

Step 6: Now to 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:

  • Name: Provide the variable name
  • value: Provide the below expression
{
"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()'),'')}
}
Power Automate flow convert xml to csv file

Step 7: Next, we will create a csv table, so, click on the +New step -> select Create CSV table action. Then provide the below information:

  • From: Select the Person details variable from dynamic content.
convert xml to csv file in Power Automate

Step 8: Next, we will send an email with csv file attachments. So, click on the +New step -> select ‘Send an email (V2)’ action. Then provide the below information:

  • To: Provide the recipient of email
  • Subject: Provide the subject of an email
  • Body: Provide the body of an email
  • Attachment name: Provide the file name
  • Attachment content: Provide the file content
convert xml to csv file in Microsoft Power Automate

Step 9: Now run the flow manually; for this, save the flow, click on the Test icon ->select the Manually option, -> click on the Test button. You can see the flow run successfully.

You can see the csv file is attached to an email using Power Automate.

convert xml file to csv file in Microsoft Power Automate

Conclusion

In this Power Automate tutorial, we saw an example of converting XML to CSV files using Power Automate.

You may also like:

>