Get xml element problem

I need to get all codes from my xml. How i can do it ?

“Goods” is part of “GoodsShipment”

In for each i try this: DeserXml.Element(“CU_DOCUMENT”).Element(“GoodsShipment”).Elements(“Goods”)
but the error keeps appearing “For Each: Object reference not set to an instance of an object.”

also i needed to get “DocumentID” and i got it with this code
xNameSpaces = “{”+DeserXml.Root.GetDefaultNamespace.ToString+“}”
DeserXml.Root.Element(xNameSpaces+“DocumentID”).Value

but that dont work for my “For each”, i try string like this DeserXml.Root.Element(“GoodsShipment”).Elements(“Goods”)

xml text:

<CU_DOCUMENT xmlns:cat_cu="*****" xmlns="*****" xmlns:cu="*****" DocumentModeID="*****">

<cat_cu:DocumentID>123456789</cat_cu:DocumentID>

<AAA>firstString</AAA>

<GoodsShipment>

<Goods>

<cu:GoodsNumeric>1</cu:GoodsNumeric>

<cu:Code>1283</cu:GoodsDescription>

<Goods>

<cu:GoodsNumeric>1</cu:GoodsNumeric>

<cu:Code>2351</cu:GoodsDescription>

<Goods>

<cu:GoodsNumeric>1</cu:GoodsNumeric>

<cu:Code>1654</cu:GoodsDescription>

Hi @Arettu63

Could you please provide the xml text, then it will be easier for us to extract the required data easily.

Hey @Arettu63

You can Iterate Through ‘Goods’ Elements:
In your ‘For Each’ activity, you should use the correct namespace to access the “Goods” elements.

DeserXml.Descendants(cuNamespace + “Goods”)

Inside the ‘For Each’ loop, use an ‘Assign’ activity to extract the ‘Code’ value. You can access it using something like item.Element(cuNamespace + "Code").Value

we would handle the XML Namespace

Thank you! now i can see the “Goods” elements, but i cant output the code.
Im use an ‘Assign’ and item.Element(cuNamespace + “Code”).Value inside it, error: Object reference not set to an instance of an object.

check that the sample / XML is valid
grafik
we do see an open Code but not closing the element
and a closing GoodDescription but dont see the opening tag

However use the immediate panel and analyze and prototype it further
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

I apologize, i misspelled it.
Currect text

<Goods>

<cu:GoodsNumeric>1</cu:GoodsNumeric>

<cu:Code>1283</cu:Code>

<Goods>

<cu:GoodsNumeric>1</cu:GoodsNumeric>

<cu:Code>2351</cu:Code>

<Goods>

<cu:GoodsNumeric>1</cu:GoodsNumeric>

<cu:Code>1654</cu:Code>

Something is wrong with your xml.
Opening and closing tag is different:
<cu:Code>1283</cu:GoodsDescription>

@Arettu63
here is solution for you:
BlankProcess66.zip (2.9 KB)

XNamespace:

for each:
image

3 Likes

i got result, but i wrote a line on my own. In for each item.Element(“cu_namespace” + “Code”).Value

There is a line at the top of the document:
<CU_DOCUMENT xmlns:cat_cu=“" xmlns="” xmlns:cu=“" DocumentModeID="”>
to refer to a “DocumentID” i need to use “xmlns:cat_cu” and for “Code” to “xmlns:cu” but how do i write it in code ?

@Arettu63 Try this solution:
BlankProcess66.zip (2.9 KB)
image

That`s the solution, thank you! But what if i dont know the specific prefix? Is there any way to recognize it ?

I think that you can use it:

3 Likes

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