Skip to content

Write to a file

This article explains how to write XML data that you have retrieved or created to a file.

This article uses the “Write XML to file” action.

Details of the “Write XML to file” action

Section titled “Details of the “Write XML to file” action”

The action has the following parameters.

Parameter Value
File path The name of the file to write to (created if it doesn’t exist)
XML to write The XML data to write
Encoding The character encoding used to output the XML data
Format XML Turn on to reformat the data being written
Indentation per level The number of spaces used to indent nested nodes

action

Depending on the encoding you choose, characters may become garbled.

The options are “System default” (which may appear as “Default” in the dialog), “ASCII”, “Unicode”, “Unicode big endian”, and “UTF-8”.

In general, we recommend choosing “UTF-8”. If the file needs to be opened in older software that assumes Shift_JIS (the system default on Japanese-language Windows), choose “System default”.

Add the sample flow by copying and pasting

Section titled “Add the sample flow by copying and pasting”

As a sample, this article uses a flow that gets Google’s sitemap information (XML) and writes it to a file.

Copy the following code and paste it into the Power Automate Desktop flow designer to add the actions.

Web.InvokeWebService.InvokeWebService Url: $'''https://www.google.com/sitemap.xml''' Method: Web.Method.Get Accept: $'''application/xml''' ContentType: $'''application/xml''' ConnectionTimeout: 30 FollowRedirection: True ClearCookies: False FailOnErrorStatus: False EncodeRequestBody: True UserAgent: $'''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20100312 Firefox/3.6''' Encoding: Web.Encoding.AutoDetect AcceptUntrustedCertificates: False Response=> WebServiceResponse StatusCode=> StatusCode
IF StatusCode <> 200 THEN
EXIT Code: 0 ErrorMessage: $'''Stopped processing because the XML data could not be retrieved.'''
END
XML.WriteXmlToFile.WriteToFileFormatted File: $'''C:\\Users\\Public\\Documents\\google-sitemap.xml''' Xml: WebServiceResponse Encoding: XML.FileEncoding.DefaultEncoding Indentation: 2

Note that this sample sets “Encoding” to “System default” (which may appear as “Default”). If you write XML that contains non-ASCII characters, such as Japanese text, we recommend changing it to XML.FileEncoding.UTF8 (“UTF-8”).

  1. In “Search actions”, type “Write XML to file”.

  2. Set the parameters as follows.

    Parameter Value
    File path C:\Users\Public\Documents\created-by-pad.xml
    XML to write <?xml version="1.0" encoding="UTF-8"?><sample>This is sample data</sample>
    Encoding UTF-8
    Format XML On
    Indentation per level 2
  3. When you run the flow, the XML data is output to the specified file path.

The text value specified as the XML data is written on a single line, but if you turn on “Format XML”, line breaks and indentation are inserted in the appropriate places.

An error occurs if the directory in the specified file path is invalid or if writing the XML fails.

We recommend adding error handling with an “On block error” block.