XPATH return result - Object


I have tested an XPath String to ensure it returns a value on http://www.freeformatter.com/xpath-tester.html#ad-output. I am now trying to use this in the Execute XPath activity in UI Path but the output is an object. Im having trouble casting this object to anything useful, can you provide any assistance on using XPath


using this instead

CassXML.XPathSelectElement(“Main/Contract[Type = ‘F’]”).Attribute(“Yr”).Value

@Andrew.James, were you able to cast the object?

The solution is the following, considering that you stored the result of the Execute XPath in the variable ‘xmlValue’ and that the result is an element:

Ctype(Ctype(xmlValue,IEnumerable(Of object)).First(),XElement).Value


Bruno Costa


I’m having an issue casting the object returned from the xpath… here is what I’m doing…

  • using XPath activity
  • I tested my xpath string and it works fine in tools outside
  • the Xpath Activity executes successfully and I casted to xmlElement variable which is “object” type because xpath only support object data type as output
  • I’m trying to find the count or value of the elements but I’m not able to get anything… I tried the above Ctype in the message box to see the value… and it gives me the error of “Object reference not set to an instance of an object”

can anyone suggest how to cast the xpath result so that I can iterate the elements

Hi @hangon,

Unfortunately from your information I can’t say what the problem is.
Could you include more details? Perhaps the cast you are trying to perform…

Just to say in advance, it’s better to test each part and see where you are getting a null reference, for instace:

Considering xmlValue as the variable returned from xPath, test only “Convert.ToString(Ctype(xmlValue,IEnumerable(Of object)).FirstOrDefault())” , if this returns empty string, then you probably need to check if the xmlValue has any value…

Hi i had an issue regarding the same problem, the text i am extracting from the XML is in Norwegian and when i convert it to string i get some norwegian letters like this :

Do you know how to fix that @brunoazev with the code you already provided?

1 Like

I had similar encoding issue when using HTTP Request to get the XML.
If you’re using Read Text File, you can add “ISO 8859-1” in the encoding field and it should fix the issue.


1 Like

Thank you @Topi that worked! :slight_smile:

Hello Bruno ,

can you please tell me if I have more than one values returned then how can put all the values in a list?

this works for me and give the first value

Ctype(Ctype(xmlValue,IEnumerable(Of object)).First(),XElement).Value