How to best input variables into xml file

Hi all. I’m looking for the best way to handle my scenario.
I need to create a reusable activity to help us create XML files.

The idea is:

  1. Send in a data table (extracted from an excel sheet).
  2. Get the text from a template XML file
  3. Replace all variable names with the values of the matching column names from the data table
  4. Save as a new text file (could save as a string variable but I worry if it would be too long. I’m guessing that a text file would be safer).

I’m looking for help with point #3.

I could replace each xml variable name with the data table value but that will be very cumbersome to code (data table will have a lot of columns) and needlessly repetitive (there will be many templates to create this way).
There has to be a way to match the variable names with data table columns programmatically without having to write each one down, right?

Here’s a snippet of one of those xml files to illustrate:

<snx>
	<unit id="${Unit}" category="${Category}" restow="NONE" transit-state="${T-state}" freight-kind="${FreightKind}" unique-key="${Unit}" line="${LineOp}">

${} contain the variables. (these variables don’t have to be saved like that, I can change that format)
I have matching data table values and would get their values like this:
dt_testData.Rows(1).Columns(“Unit”).ToString

How can I loop through the file and replace all of those variables?

Hello @Maria_Concepcion and welcome to the UiPath Community Forum

I hope that I understand the problem correctly.

I would use a For Each Row in Data Table activity to loop the datatable with the values to be inserted in your XML Template.
Inside this loop I would use a For Each activity to loop the columns and replace the placeholder with the value of the cell:

str_XMLTempplate.Replace("{"+currentDataColumn.ToString+"}",CurrentRow(currentDataColumn.ToString).ToString)

Regards
Soren

1 Like

This looks interesting. I’ll test it out and let you know if it works as expected. Thanks.

For anyone interested, I used a combination of what @SorenB suggested and a simple Find and Replace and it worked like a charm.

  1. Read the xml file and save it as a xmlString
  2. For Each row in data table
  3. Inside it, For each currentDataColumn
  4. Inside it, for sanity sake, I saved current column name inside a variable colName = currentDataColumn.ToString
  5. Still inside for each column, I added a find and replace

  1. Then write xmlString to a new file
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.