HELP - Get Value of Tags of XML NFe

Hello, can someone help me understand why my code isn’t working?

I’m trying to read each ‘nLote’ tag contained in the XML of an NFe (Nota Fiscal eletrônica, Brazilian Electronic Invoice), but when I execute the code, I get the following error: ‘Assign: Object reference not set to an instance of an object.’"

for each: xmlDeserialized.Element(ns+“nfeProc”).Element(ns+“NFe”).Element(ns+“infNFe”).Element(ns+“det”).Elements(ns+“prod”).Elements(ns+“rastro”)

Assing:
Resultado = currentXElement.Element(“nLote”).Value.ToString

XML: /nfeProc/NFe/infNFe/det/prod/rastro/nLote

Hi,

If possible, can you share your xml file?

Regards,

I can’t share files because I’m a new user, but here’s an image:

I managed to fix the error, but now it only returns the result to me once, and it should be correct five times, as I have five items.

For Each: xmlDeserialized.Element(ns+“nfeProc”).Element(ns+“NFe”).Element(ns+“infNFe”).Element(ns+“det”).Elements(ns+“prod”).Elements(ns+“rastro”).Elements(ns+“nLote”)

Resultado = currentXElement.ToString

Hi,

I think it’s necessary to get collection of “det” then iterate it, and get nLote in the iteration.

xmlDeserialized.Element(ns+“nfeProc”).Element(ns+“NFe”).Element(ns+“infNFe”).Element(ns+“det”)

Regards,

How do I can do this?

Hi,

How about the following?

xmlDeserialized.Element(ns+"nfeProc").Element(ns+"NFe").Element(ns+"infNFe").Elements(ns+"det")

currentItem.Element(ns+"prod").Element(ns+"rastro").Element(ns+"nLote").Value

Regards,

It worked, thank you very much!

1 Like

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