Skip to main content

Adding elements

This time, I will explain how to add elements to existing XML files and XML data.

Action to use

This time, the action to use is "Insert XML elements".

"Insert XML elements" action details

The action has the following parameters.

Parameter namevalue
XML DocumentsTarget XML Data (XML Node Object Must be)
XPath QuerySpecify the insertion destination of the element in the XPath method
Insert XML elementInsert XML data

action

Implementing action

Add a sample flow with copy and paste

This time, as a sample, we have prepared a flow to save XML data in file, get data again and add elements.

Copy the following code and paste it on the edit screen of Power Automate Desktop to add the target action.

SET FilePath TO $'''C:\\Users\\Public\\Documents\\sample.xml'''
XML.WriteXmlToFile.WriteToFileFormatted File: FilePath Xml: $'''<sample><data><content>Test data</content></data></sample>''' Encoding: XML.FileEncoding.UTF8 Indentation: 2
XML.ReadFromFile File: FilePath Encoding: XML.FileEncoding.UTF8 XmlDocument=> XmlDocument
XML.InsertElement Document: XmlDocument XPathQuery: $'''/sample/data''' Element: $'''<insert>Content added from PAD</insert>'''

Before execution, XML is as follows.

<sample>
<data>
<content>Test data</content>
</data>
</sample>

After execution, it changes as follows.

<sample>
<data>
<content>Test data</content>
<insert>Content added from PAD</insert>
</data>
</sample>

Operate and implement Power Automate Desktop

  1. Enter "Insert XML elements" from "Action Search".

  2. Set the parameter as follows

    Parameter namevalue
    XML document%XmlDocument%
    XPath Query/sample/data
    Insert XML element<insert>Content added from PAD</insert>
  3. By execution, the element specified in the target XML data is added.

The exception that can occur

If the XPath format is incorrect or an invalid XPath is specified, an error will occur.

Even if there is no error in the format, if the specified element does not return the value, it will be an error.

Also, as an exception, an error may occur when insertion.

We recommend that you implement block processing.