Query XML file

Hello,

I have an XML file like this:

<root>
<item><country> Portugal</country><city><Lisbon></city></item>
<item><country> Spain</country><city><Madrid></city></item>
<item><country> France</country><city><Paris></city></item>
<item><country> Germany </country><city><Berlin></city></item>
<item><country> Netherlands</country><city><Amsterdam></city></item>
</root>

Process short description: In the form, I will have a dropdown list with the countries from the XML file. Based on the selection from the form, I will want to extract the corresponding city.

What do I have currently? At the moment, I have a For Each cycle to run all the country elements and add it to a list. Then the Form appears with the countries dropbdown list and it saves the selected option into a variable (selectedOption). Based on the result, I am using another For Each cycle to check if the actual for each element equals the selected form option or not. This solution works fine.

Optimization suggestions: However, I would like to know if this is possible or how to do a query in XML . Something like:

FROM country in xmlFile.Element("root").Element("item").Element("country").Value
WHERE xmlFile.Element("root").Element("item").Element("city").Value.Equals(selectedOption)
SELECT country

Can someone help? :slight_smile:

I am not sure I understand the process completely…
But regarding the optimization:
I don’t know how to query to an XML-file. But if you do have the XML-file available I would use Regex to get it into a datatable [“Country | City”] and use datatable.select

@Fer
Your requests can be served e.g. with Linq to XML.

From the UiPath.web.activities packages the deserialize XML Activity will give the Option to parse an XML String and Returns a xdocument

on this xdocument you can use the Provided API or also writing the linq Statement within the Query Syntax looking very Close to your question idea.

@ppr, can you give an example?

@Fer
Yes, Just Let me Return from my Business Trip and Then i will do

You can try reading your xml to a Data Table and then will be easier to handle filters and everything… use myDataTable.ReadXml(“c:\myfile.xml”) after initializing an empty data table variable.

@bcorrea, yes that using a datatable also works. My question was regarding if it is possible to do something like a query with XML or not :slight_smile: Thanks for your suggestion

I mean, after you have it as a Data Table, running queries will be a lot easier…

@bcorrea, is it not possible to do within an assign?

If you are talking about converting xml to data table, then you need an Invoke Method activity.

@Fer
PFA sample Demo with this play game:

all cities will be retrieved and be used within a iinput message for selection:
grafik

After the retrieval of the country based to the selected city it will be displayed like this:
grafik

PFA demo xaml and xml here:
Fer.xaml (6.1 KB)
sample.xml (456 Bytes)

Kindly note:

  • ensure uipath.web.activites is added to the dependencies
  • ensure following imports are available:
    grafik
  • your XML sample was corrected to valid XML regarding the city information

for further information also have a look here:

@ppr, instead of using the input dialog, how can you do it by using the create form activity (package: UiPath.Form.Activities)

image

@Fer
Lets do IT step by step. Accordingly to the topic title, the Filtering and retrieval of country is working for you as expected?

What are the requirements for the Forms. Only showing the Combo Box with the cities or anything else additionaly?

@ppr, it was giving an error when using the Form. I will try again. Thank you for your help :smiley: