How to get xml specific data

i have xml data in text file. below

<?xml version="1.0" encoding="UTF-8"?> Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 Learning XML Erik T. Ray 2003 39.95

how to get specific like ->book category=“children” , in that ->author name ?

could you please help me
bookstroeXMl.txt (916 Bytes)

hII @anand_kumar4

You can use XDocument to read the XML and get the author for the book where category="children".

Step 1

Use Read Text File and store the output in a String variable:

xmlText

Step 2

Import:

System.Xml.Linq

Step 3

Use an Assign activity:

xmlDoc = XDocument.Parse(xmlText)

Create xmlDoc as type:

System.Xml.Linq.XDocument

Step 4

Use another Assign to get the author:

authorName = xmlDoc.Descendants("book").
    Where(Function(x) x.Attribute("category")?.Value = "children").
    Select(Function(x) x.Element("author")?.Value).
    FirstOrDefault()

Create authorName as String.

Step 5

Use Write Line:

authorName

This will return the author name from the book whose category is children.

Note: The text shared in the question doesn’t show the XML tags such as <book>, <author>, etc. Please make sure the actual .txt file contains valid XML before using XDocument.Parse().

a few of many options:

parse the XML with Deserialize XML - out Variable: xDoc

LINQ:

XPath:

ensure that:


is imported

Getting from all relevant book elements the author:
e.g.

error–[Error] Assign: The value cannot be an empty string. (Parameter ‘expandedName’)

autorname i given variable type -object
XML _Data_Extraction -2.xaml (12.8 KB)

was not mentioned above

as the screenshot came from immediate panel you also have a reference of working statement

xDoc.Descendants("book").Where(Function (x) x.Attribute("category").value.equals("children")).ToList

Hi @anand_kumar4,

UiPath provides a set of XML activities to read and query XML data. You can use Deserialize XML and then use an XPath expression to retrieve the required node.

You can refer to the official documentation here:

Thanks!

@anand_kumar4

The issue is because .ToList() returns a list, while authorname is a String variable.

You can use the following expression instead:


authorname = xdoc.Descendants("book").
    Where(Function(x) x.Attribute("category").Value = "children").
    Select(Function(x) x.Element("author").Value).
    FirstOrDefault()

Make sure authorname is of type String.

For the given XML, the output will be J K. Rowling.

Hope this helps!

Hi @anand_kumar4

one small thing worth adding for robustness, the final expression dropped the ?. that was in Mayuresh’s first answer (x.Attribute(“category”).Value instead of x.Attribute(“category”)?.Value). If any book element in your real file doesnt have a category attribute at all, that line throws a NullReferenceException instead of just skipping it. Worth putting the ?. back just in case your production xml isnt as clean as this sample

also if you ever need more than one book matching (say two “children” books), swap FirstOrDefault() for ToList() and authorname becomes a List(Of String) instead of a plain String, since right now you’ll only ever get the first match.

hope this helps down the line