How to Create XML file from CSV in Power Automate?

In this Power Automate tutorial, we will create an XML file from CSV data using Power Automate or Microsoft Flow.

For example, I have a CSV file in the SharePoint library, and we will see how we can create or convert the XML file, like below.

How to create xml file from csv using Power Automate

So, we will create an Instant cloud flow; then, we will get the file content of the CSV file. Then we will convert the file content base64 to string, we will parse the CSV, and convert the parsed CSV content to XML using XML(). Then we will send this file content as an attachment to an email.

How to create XML file from CSV using Power Aautomate

Here we will see how to create an XML file from CSV using Power Automate.

Let’s create an XML file from CSV using Instant Cloud flow using Power Automate.

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

How to create xml file from csv using MS Power Automate

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

create xml file from csv using MS Power Automate

Next, you can see Manually trigger a flow action is added to the flow page.

Power automate csv to xml

Step 2: Now we will get the file content using path, so, click on the +New step and select ‘Get File content using path‘ action. Then provide the below information:

  • Site address: Select the SharePoint site address.
  • File path: Provide the file path of the CSV file.
Power automate convert csv to xml

Step 3: Now we will convert base64 to string using Power Automate, so click on the +New step -> select Compose action. Then provide the below information:

  • Inputs: Provide the below expression:
string(outputs('Get_file_content_using_path')?['body'])
Power automate convert csv file to xml

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

  • Inputs: We will provide the line break, so, click on enter.
Power automate convert csv to xml file

Step 5: Next, we will split rows of data with a line break, so, click on the +New step -> select Compose action. Then provide the below information:

  • Inputs: Then provide the below expression:
split(outputs('Compose-_base64_to_string'),outputs('Compose_-Line_break'))
Power automate create xml file from csv

Step 6: After that, we will get the header of the CSV table, so, click on the +New step -> select Compose action. Then provide the below information:

  • Inputs: Provide the below expression:
split(first(outputs('Compose_-split_rows')),',')
Microsoft Power automate create xml file from csv

Step 7: Now, we will create an array of objects by mapping the key and value in the Select action. So, click on the +New step -> select the ‘Select’ action. Then provide the below information:

  • From: Provide the below expression
skip(outputs('Compose_-split_rows'),1)
  • Map: In map provide the below expression:
{
  "@{outputs('Compose_-get_field_name')[0]}": @{split(item(),',')?[0]},
  "@{outputs('Compose_-get_field_name')[1]}": @{split(item(),',')?[1]},
  "@{outputs('Compose_-get_field_name')[2]}": @{split(item(),',')?[2]},
  "@{outputs('Compose_-get_field_name')[3]}": @{split(item(),',')?[3]}
}
Microsoft flow create xml file from csv

Step 8: Now we will remove the last item i.e. blank item from an array, so, click on the +New step -> select Compose action. Then provide the below information:

  • Inputs: Provide the below expression:
take(body('Select'),add(length(body('Select')),-1))
How to create xml file from csv Microsoft flow

Step 9: Now we will convert it to JSON, then convert it to XML, so, click on the +New step and select Compose action. Then provide the below information:

  • Inputs: Provide the below expressions:
xml(json(concat('{"username": {"id": [',join(outputs('Compose-remove_last_blank_item'),','),']}}'))) 
How to create xml file from csv using Microsoft flow

Step 10: Now we will send an email with the email attachment, so, click on the +New step -> select Send an email (V2) action. Then provide the below information:

  • To: Provide the recipient’s email.
  • Subject: Select the subject of an email
  • Body: Provide the body of an email.

After that, click on Show Advanced options.

  • Attachment name: Provide the Attachment name.
  • Attachment content: Provide the output from the dynamic content.
How to create xml file from csv using Microsoft Power Automate

Step 11: Now, to test the flow, click on Save and then click on the Test icon ->select the Manually option -> click on the Test button. You can see your flow run successfully.

You can see the CSV file is converted to an XML file.

How to create xml file from csv using Power Automate flow

Conclusion

In this Power Automate tutorial, we saw how to convert a CSV file to an XML file using Power Automate.

You may also like:

  • Hi! the select does not work, when I use the recommended input I received the following error message:

    The execution of template action ‘Select’ failed: The evaluation of ‘query’ action ‘where’ expression ‘{
    “@{outputs(‘Compose_-get_field_name’)[0]}”: “@{split(item(),’,’)[0]}”,
    “@{outputs(‘Compose_-get_field_name’)[1]}”: “@{split(item(),’,’)[1]}”,
    “@{outputs(‘Compose_-get_field_name’)[2]}”: “@{split(item(),’,’)[2]}”,
    “@{outputs(‘Compose_-get_field_name’)[3]}”: “@{split(item(),’,’)[3]}”
    }’ failed: ‘The template language expression ‘split(item(),’,’)[1]’ cannot be evaluated because array index ‘1’ is outside bounds (0, 0) of array.

    do you know how might I fix this in order to complete the flow and convert the csv to xml?

    thanks in advance
    sofia

  • >