Object reference not set to an instance of object error while processing xml file

Hello Team,

After deserializing the XML file I printed the whole data in a message box next to it I used write line activity. At write line activity it was showing an “Object reference not set to an instance of an object error”. Here is the attached file for reference. samplexaml.zip (6.3 KB)
Could anyone suggest, please

Your workflow looks good except that in case of xmls like any other tool the processor requires the namespace of the element to identify the correct element.

What you need to do.

  1. Create a variable ns of type XNamespace and set it default value to “http://www.portalfiscal.inf.br/nfe” which is your xmls default namespace
  2. In the write line where you are using .Element(“nfeProc”) you need to actually use .Element(ns+“nfeProc”)… like this for all your element.

Your writeline:
Doc.Element(xs+"nfeProc").Element(xs+"NFe").Element(xs+"infNFe").Elements(xs+"ide").Count.ToString

4 Likes

Thank you Nadim. Its working. May I know why namespaces required here as if I am not much aware of XML

Let me try and brief it out.

XML elements in a file need to be uniquely identified to avoid issues when you have same type of elements but they differ in usage (say ID of SAP and ID of Oracle. Both are same elements but belong to different classes/instances).
To do that there is a standard of using namespaces.
Namespaces can be single(when you do just xmlns) like in your case or many(when you use prefix xmlns:ns1).

So when you are using the .Element structure you are basically the processor that this is the path(xpath or xml-path) to the element that i want. nfeProc/NFe/infNFe/ide Its like a folder structure.

Now that there is a namespace for your elements that is defined in the parent element you will need to prefix that for the elements otherwise it will not be able to identify it and thus gives you the error “Object reference not set to an instance of an object error” because its empty.

Please close this thread marking the answer as a solution to this thread.

Thanks, Nadim. I have another small doubt regarding queues and transactions. I am able to retrieve the single item from Get Transaction Item activity (item.Specificcontent(“fieldname”). Can I do the same thing using Get Queues item activity if so could you plz let me know what is the command to get the item.

Actually When you will use GetQueuItems you will get the list of items that you want to extract and then you can iterate through the and get the required data out of each.

Detailed info can be found here:

Example of how to use this activity is here:

Nadim… I’m facing the exact same problem here, but my namespaces are differents! And, I really dont know how to get it!

Can you help me out to identify how to get the arrowed value, please?

I would appreciate your help!

1 Like

Well you dont have to worry if they are different. You just need declare it like we do in case of processing xmls.
Create the various namespaces with the prefix you desire and then use them in the expression like i have mentioned.

In your case you have XNamespace vars
ns1 = Atom Syndication Format namespace
ns2 = http://schemas.microsoft.com/ado/2007/08/dataservices/metadata
ns3 = http://schemas.microsoft.com/ado/2007/08/dataservices/

and your exp would be:
Doc.Element(ns1+“entry”).Element(ns1+“content”).Element(ns2+“properties”).Elements(ns3+“ArquivoCriado”).ToString`

You can tweak as required. :slight_smile:

1 Like

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