Get certain attributes value from XML file


#1

Hi,

Im not too familiar with XML. Here’s the part of the XML-document:

I would like to get the value of the ‘native’ attribute in the ‘callid’ -element.

I have tried this on Write Line, after using the Deserialize Xml -Activity:
resultXML.Element(“callid”).Attribute(“native”).Value.ToString and many other variations of this. It doesn’t work. Only gives this error: Write line : Object reference not set to an instance of an object.

Is there a way I could get the value of the attribute quickly and easily with a command like that?

I have tried other solutions found on this forum, but they don’t wor for this file for some reason.

EDIT: also I’ve tried the execute XPath activity with: "//callid/@native"
It gives the result as an Object-variable. Is there a way I could print that as a string?


Fetch XML node value (with condition on other node values) using Execute Xpath utility in UIPath
#2

Hello!

You can use XPATH.

Try something like this:

image


#3

Thanks, but it doesn’t work for me.

What should be the TypeArgument in the For each -loop?

This is what it looks like:

Is there something wrong with my XPath command? The execution never even enters the for each loop. The variable ‘xmlValue’ is the result from the ‘Execute XPath’ activity.


#4

The TypeArgument from For Each Loop must be “System.Xml.Linq.XElement

On the Execute XPath, try this: “recording filename/cti segmentnum

Then you use: “item.Element(“callid native”).Value.ToString()

Regards,


#5

Have you tried this XPath:

string(//callid/@native)

[EDIT:]
I just tried this:
image

This was the xml on which i tried:

<rootElem>
	<elem>
		<prop1 attrib1="val1" attrib2 = "val2" />
		<prop2 attrib1 = "Val1" /> content <prop2/>
	</elem>
	<elem>
		<prop1 attrib1="val11" attrib2 = "val21" />
		<prop2 attrib1 = "Val11" /> content2 <prop2/>
	</elem>
	<elem name = "name1">
		<prop1 attrib1="val1" attrib2 = "val2" />
		<prop2 attrib1 = "Val1" /> content <prop2/>
	</elem>
</rootElem>

Got this result:
image


#6

I have now. The XPath result is still an Object -variable. How do you print that? I tried result.ToString but it only prints this:
System.Xml.XPath.XPathEvaluator+d__1`1[System.Object]

Thank you both for the help anyways!


#7

can you share the xml? i’ll try building the xpath for it


#8

Unfortunately I cannot share the file. I’ve tried the Get XML Nodes and Get XML Node Attributes -activities and I was able to find the right attribute in a string format.

Thanks anyway for the help!


#9

Hi Akhi, I would like to know how I write in the
Execute XPath to an xml field. Example in Picture:
image