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 name | value |
---|---|
XML Documents | Target XML Data (XML Node Object Must be) |
XPath Query | Specify the insertion destination of the element in the XPath method |
Insert XML element | Insert XML data |
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
Enter "Insert XML elements" from "Action Search".
Set the parameter as follows
Parameter name value XML document %XmlDocument% XPath Query /sample/data
Insert XML element <insert>Content added from PAD</insert>
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.