Unable to get data from XML

I’m trying to read every SellersItemIdentification from each InvoiceLine. I made a for each loop based on the example found on https://www.uipath.com/kb-articles/how-to-iterate-and-read-xml-documents.
But it doesn’t work with the XML we receive because the “:” can’t be used in the name.
I already found some info about using XNamespace but i can’t get it to work.
How do I get this to work?

This is part of the XML we receive:

This is the for each loop I have at the moment:
UIPath

Thanks!

Hi @JanWillem
It’s ll sure help you :+1:

On the use of namespaces, you can simply add the namespace as a variable in your string when trying to access data in the XML, leave out the “:”.

xmlDoc.Element(activities+"Activity").Attribute(x + "Class").Value

X in the above is a XNamespace variable:

x = “http://schemas.microsoft.com/winfx/2006/xaml

I don’t know if that is your issue exactly.

I use the structure of the example so I have the following steps:

  • Read Tekst File

  • Deserialize XML

  • Assign the variable
    cac = “urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2”

  • For each Item in XML.Element(“Invoice”).Elements(cac+“InvoiceLine”)

But then I get another error:
error

Can you see what I’m doing wrong based on the XML I posted?

Thanks for the answer but when I don’t see which part of the post I should use for my problem.

First off, make sure you are passing the correct TypeArgument to your ForEach loop:

Then start by just trying to access the parent node of the XML so just write your

ForEach Item In xmlDoc.Elements

Write Line > Element.tostring

That should export the entire XML to the output panel.

Then start adding added definition to your In argument

xmlDoc.Elements(namespacevar+“ElementName”).Elements(namespacevar2+“ElementName”).Elements

This will pass a number of system.xml.linq.XElement variables to your for each loop and doing it one by one ensures easy troubleshooting.

Within elements you will have attributes and these attributes have values in quotation marks. if you are trying to access these values directly then it will be something like this passed into the for each:

xmlDoc.Elements(namespacevar+“ElementName”).Elements(namespacevar2+“ElementName”).Attributes

Then in your For Each action pane:

item.value.ToString

You may find a site like this useful to provide your XML tree structure in a more easily understood format for this task: https://codebeautify.org/xmlviewer

share the xml file?

Thanks @ronanpeter!

1 Like

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